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

本文介绍了四道来自Codewars的JavaScript练习题目,包括情绪计算(Paul's Misery)、错位复印机(The Office III)、找会议室(The Office IV)和猫鼠游戏(Cat and Mouse Easy Version),展示了如何通过编程解决实际问题。

codewars-js练习

2021/4/8

github 地址

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值