CodeWARS Exercise (JavaScript) -2021/4/9

tags: javascript  codewars  

CodeWars-JS exercise

2021/4/9

Github address

My Github Address, I have the exercise record on it, constantly updating ...

【1】<7kyu>【Slaphead】

Shaving the clean hair "-" means that the scattered hair is "/". Your task is to check if the head has scattered hair and removes them.

example

0 hairs --> "Clean!"
1 hair --> "Unicorn!"
2 hairs --> "Homer!"
3-5 hairs --> "Careless!"
>5 hairs --> "Hobo!"


 "------/------" you shoud return:
["-------------", "Unicorn"]

solution

<script type="text/javascript">
	function bald(x){
		console.log(x)	
		var len = x.length;
		var s = x.match(/[/]/g);
		var temp = [];
		if(s == null) return [x,'Clean!']
		var num = s.length;
		for(var i=0;i<len;i++){
			temp.push('-');
		}
		var str = temp.join('');
		var result = [str];
		if(num == 0)result.push('Clean!');
		else if(num == 1)result.push('Unicorn!')
		else if (num == 2)result.push('Homer!')
		else if (num>=3&& num<=5)result.push('Careless!')
		else result.push('Hobo!')		
		return result;
	}
	// verify
	// console.log(bald('/---------'));// ['----------', 'Unicorn!']
	// console.log(bald('/-----/-'));//['--------', 'Homer!']
	// console.log(bald('--/--/---/-/---'));// ['---------------', 'Careless!']
	console.log(bald('-----'));//['----','Clean!']
</script>

【2】<6kyu>【Handshake problem】

Johnny wants to understand the number of participants this year by calculating all the number of handshakes.

This function acquires the number of handshakes and returns the minimum number of minimal people required to perform these handshakes (a pair of farmers only shake it once).

Hold a handshake Minimum number of people
0 1
1 2
3 3
6 4

example

6//4
7//5

solution

<script type="text/javascript">
	function getParticipants(handshakes){
		// console.log(handshakes)
		x = (1 + Math.sqrt(1+8*handshakes))/2
		return Math.ceil(x)
	}
	// verify
	console.log(getParticipants(0));// 1
	console.log(getParticipants(1));// 2
	console.log(getParticipants(3));//3
	console.log(getParticipants(6));//4
	console.log(getParticipants(7));//5
</script>

Intelligent Recommendation

CodeWARS Exercise (JavaScript) -2021/4/11

Article catalog CodeWars-JS exercise 2021/4/11 Github address 【1】<8kyu>【Thinkful - Logic Drills: Traffic light】 【2】<8kyu>【Powers of 2】 【3】<6kyu>【All Star Code Challenge #15】 CodeWars...

CodeWARS Exercise (JavaScript) -2021/4/12

Article catalog CodeWars-JS exercise 2021/4/12 Github address 【1】<7kyu>【Easy wallpaper】 【2】<6kyu>【Rainfall】 CodeWars-JS exercise 2021/4/12 Github address My Github Address, I have the exer...

CodeWARS Exercise (JavaScript) -2021/4/13

Article catalog CodeWars-JS exercise 2021/4/13 Github address 【1】<5kyu>【Calculating with Functions】 【2】<7kyu>【All Star Code Challenge #22】 【3】<7kyu>【All Star Code Challenge #24】 【4】&...

CodeWARS Exercise (JavaScript) -2021/4/14

Article catalog CodeWars-JS exercise 2021/4/14 Github address 【1】<8kyu>【Do you speak "English"?】 【2】<7kyu>【Filter Long Words】 【3】<6kyu>【English beggars】 【4】<8kyu>【Dri...

CodeWARS Exercise (JavaScript) -2021/4/15

Article catalog CodeWars-JS exercise 2021/4/15 Github address 【1】<7kyu>【Drone Fly-By】 【2】<8kyu>【Abbreviate a Two Word Name】 【3】<7kyu>【Substituting Variables Into Strings: Padded Numb...

More Recommendation

CodeWARS Exercise (JavaScript) -2021/4/25

Article catalog CodeWars-JS exercise 2021/4/25 Github address 【1】<6kyu>【Twisted Sum】 【2】<6kyu>【Most profit from stock quotes】 CodeWars-JS exercise 2021/4/25 Github address My Github Addres...

CodeWARS Exercise (JavaScript) -2021/5/6

Article catalog CodeWARS-JS exercise 2021/5/6 Github address 【1】<7kyu>【Greatest common divisor】 CodeWARS-JS exercise 2021/5/6 Github address My Github Address, I have the exercise record, consta...

CodeWARS Exercise (JavaScript) -2021/1/18

Article catalog CodeWARS-JS exercise 2021/1/18 CodeWARS-JS exercise 2021/1/18 【1】In this kata you will create a function that takes a list of non-negative integers and strings and returns a new list w...

CodeWARS Exercise (JavaScript) -2021/1/19

CodeWARS-JS exercise 2021/1/19 【1】Write a program that finds the summation of every number from 1 to num. The number will always be a positive integer greater than 0.(Ie 1 to NUM) example: solution: 【...

CodeWARS Exercise (JavaScript) -2021/1/20

Article catalog CodeWars-JS exercise 2021/1/20 Github address 【1】 <6kyu>【Build Tower】 【2】<6 kyu>【Replace With Alphabet Position】 【3】<8 kyu>【Sum of positive】 【4】<8kyu>【Calculate...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top