文章目录
codewars-js练习
2021/3/8
github 地址
【1】<8kyu>【Find Maximum and Minimum Values of a List】
example:
max([4,6,2,1,9,63,-134,566]) returns 566
min([-52, 56, 30, 29, -54, 0, -110]) returns -110
max([5]) returns 5
min([42, 54, 65, 87, 0]) returns 0
solution
<script type="text/javascript">
var min = function(list){
list.sort((a,b)=>{return a-b;})
return list[0];
}
var max = function(list){
list.sort((a,b)=>{return b-a;})
return list[0];
}
// 验证
console.log(max([4,6,2,1,9,63,-134,566]));//566
console.log(min([-52, 56, 30, 29, -54, 0, -110]));// -110
console.log(max([5]));//5
console.log(min([42, 54, 65, 87, 0]));// 0
</script>
【2】<6kyu>【Lottery Ticket】
To do this, you must first count the ‘mini-wins’ on your ticket. Each subarray has both a string and a number within it. If the character code of any of the characters in the string matches the number, you get a mini win. Note you can only have one mini win per sub array.
Once you have counted all of your mini wins, compare that number to the other input provided (win). If your total is more than or equal to (win), return ‘Winner!’. Else return ‘Loser!’.
example:
[ [ 'ABC', 65 ], [ 'HGR', 74 ], [ 'BYHT', 74 ] ]
solution
<script type="text/javascript">
function bingo(ticket, win){
// console.log(ticket,win);
var num = 0;
for(var i=0;i<ticket.length;i++){
if(ticket[i][0].indexOf(String.fromCharCode(ticket[i][1])) != -1)num ++;
}
// console.log(num)
if(num >= win)return 'Winner!';
return 'Loser!'
}
// 验证
console.log(bingo([['ABC', 65], ['HGR', 74], ['BYHT', 74]], 2));// 'Loser!'
console.log(bingo([['ABC', 65], ['HGR', 74], ['BYHT', 74]], 1));//'Winner!'
console.log(bingo([['HGTYRE', 74], ['BE', 66], ['JKTY', 74]], 3));//'Loser!'
</script>
146

被折叠的 条评论
为什么被折叠?



