文章目录
codewars-js练习
2021/3/6
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>
以上为自己思路供大家参考,可能有更优的思路。