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

分享了三道Codewars上的JavaScript练习题目:压力下编码挑战、昆虫速度换算及爱情花语判断。通过实例展示了如何处理整数加倍、速度单位转换和判断花朵配对的逻辑。

codewars-js练习

2021/3/6

github 地址

my github地址,上面有做的习题记录,不断更新…

【1】<8kyu>【You Can’t Code Under Pressure #1】

Code as fast as you can! You need to double the integer and return it.

example

2//4

solution

<script type="text/javascript">
        function doubleInteger(i) {
            console.log(i)
            return i*2

        } 
        // 验证
        console.log(2);//4
    </script>
【2】<8kyu>【Beginner Series #4 Cockroach】

The cockroach is one of the fastest insects. Write a function which takes its speed in km per hour and returns it in cm per second, rounded down to the integer (= floored).

它的速度是千米每小时,返回的是厘米每秒,

example

cockroachSpeed(1.08) == 30

solution

<script type="text/javascript">
        function cockroachSpeed(s) {
            // console.log(s)
            return parseInt(s * (1/36)*1000)
        } 
        // 验证
        console.log(cockroachSpeed(1.08));//30
        console.log(cockroachSpeed(1.09));// 30
        console.log(cockroachSpeed(0));// 0
    </script>
【3】<8kyu>【Opposites Attract】

Timmy & Sarah think they are in love, but around where they live, they will only know once they pick a flower each. If one of the flowers has an even number of petals and the other has an odd number of petals it means they are in love.

Write a function that will take the number of petals of each flower and return true if they are in love and false if they aren’t.

如果一朵花的花瓣数是偶数,另一朵花的花瓣数是奇数,这意味着他们相爱了。

example

(1,4)// true

solution

<script type="text/javascript">
        function lovefunc(flower1, flower2){
            // console.log(flower1,flower2)
            var bool1 = (flower1%2==0) && (flower2%2!=0)
            var bool2 = (flower2%2==0) && (flower1%2!=0)
            if(bool1 || bool2)return true;
            return false;
        } 
        // 验证
        console.log(lovefunc(1,4));// true
        console.log(lovefunc(2,2));// false
        console.log(lovefunc(0,1));//true
        console.log(lovefunc(0,0));//false
    </script>

以上为自己思路供大家参考,可能有更优的思路。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值