文章目录
codewars-js练习
2021/4/8
github 地址
【1】<7kyu>【Paul’s Misery】
Given an array (x) you need to calculate the Paul Misery Score. The values are worth the following points:
example:
kata = 5
Petes kata = 10
life = 0
eating = 1
< 40 = 'Super happy!'
< 70 >= 40 = 'Happy!'
< 100 >= 70 = 'Sad!'
> 100 = 'Miserable!'
solution
<script type="text/javascript">
function paul(x){
// console.log(x)
var map = {'kata':5,'Petes kata':10,'life':0,'eating':1};
var sum = 0;
for(var i=0;i<x.length;i++){
sum += map[x[i]]
}
// console.log(sum)
if(sum<40)return 'Super happy!'
else if(sum>=40 && sum<70) return 'Happy!'
else if(sum >=70 && sum<100) return 'Sad!'
else return 'Miserable!'
}
// 验证
console.log(paul(['life', 'eating', 'life']));// 'Super happy!'
console.log(paul(['life', 'Petes kata', 'Petes kata', 'Petes kata', 'eating']));// 'Super happy!'
console.log(paul(['Petes kata', 'Petes kata', 'eating', 'Petes kata', 'Petes kata', 'eating']));// 'Happy!'
</script>
【2】<7kyu>【The Office III - Broken Photocopier】
example:
"1"//"0"
"10000000101101111110011001000"//"01111111010010000001100110111"
"100010"//"011101"
solution
<script type="text/javascript">
function broken(x){
// console.log(x)
var arr = x.split('')
for(var i=0;i<arr.length;i++){
if(arr[i]==1) arr[i]=0
else if(arr[i]==0) arr[i]=1
}
return arr.join('')
}
// 验证
console.log(broken("1"));//"0"
console.log(broken("10000000101101111110011001000"));//"01111111010010000001100110111"
console.log(broken("100010"));//"011101"
</script>
function broken(x){
// console.log(x)
return x.replace(/[10]/gi, match=> match ==='0'? '1':'0');
}
【3】<7kyu>【The Office IV - Find a Meeting Room】
找到第一个空房间并返回它的索引(注意,在某些测试用例中可能有多个空房间)。
‘X’ --> busy ‘O’ --> empty
If all rooms are busy, return -1.
example:
['X', 'O', 'X']// 1
['X','X','X','X','X']// 'None available!'
solution
<script type="text/javascript">
function meeting(x){
// console.log(x)
var index = x.join('').search(/[O]/g)
if(index == -1) return 'None available!'
return index
}
// 验证
console.log(meeting(['X', 'O', 'X']));// 1
console.log(meeting(['O','X','X','X','X']));// 0
console.log(meeting(['X','X','X','X','X']));// 'None available!'
</script>
【4】<7kyu>【Cat and Mouse - Easy Version】
example:
You need to find out if the cat can catch the mouse from it's current position. The cat can jump over three characters. So:
C.....m returns 'Escaped!' <-- more than three characters between
C...m returns 'Caught!' <-- as there are three characters between the two, the cat can jump.
solution
<script type="text/javascript">
function catMouse(x){
console.log(x)
var str = x.slice(1,x.length-1);
if(str.length >3) return "Escaped!"
else if(str.length <=3) return "Caught!"
}
// 验证
console.log(catMouse('C....m'));// "Escaped!"
console.log(catMouse('C..m'));// "Caught!"
console.log(catMouse('C.....m'));//"Escaped!"
</script>
function catMouse(x) {
return x.length <= 5 ? 'Caught!' : 'Escaped!';
}
本文介绍了四道来自Codewars的JavaScript练习题目,包括情绪计算(Paul's Misery)、错位复印机(The Office III)、找会议室(The Office IV)和猫鼠游戏(Cat and Mouse Easy Version),展示了如何通过编程解决实际问题。
225

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



