codewars练习(javascript)-2021/3/8

codewars-js练习

2021/3/8

github 地址

my 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>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值