codewars练习(javascript)-2021/1/24

codewars-js练习

2021/1/24

github 地址

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

【1】<8kyu>【Sum The Strings】

Create a function that takes 2 positive integers in form of a string as an input, and outputs the sum (also as a string):

example

sumStr("4", "5")    // should output "9"
 sumStr("34", "5")   // should output "39"

solution:

<script type="text/javascript">
 		function sumStr(a,b) {
     
     
 			var sum = +a + +b;
 			console.log(sum);
 			return sum.toString();
 		}
 				
		// 验证
		console.log(sumStr("4", "5") ); //  '9'
	</script>

【2】<6kyu>【Write Number in Expanded Form】

You will be given a number and you will need to return it as a string in Expanded Form.

example

expandedForm(12); // Should return '10 + 2'
expandedForm(42); // Should return '40 + 2'
expandedForm(70304); // Should return '70000 + 300 + 4'

思路:

  • 先将num切割放入到数组中;
  • 遍历数组并进行运算,然后将非0的数字放入到新的数组中;
  • 最后将新数组中的数据通过+进行连接

solution:

<script type="text/javascript">
 		function expandedForm(num) {
     
     
 			// 将num切割放入到数组中
 			var arr = num.toString().split('');
 			// console.log(arr);
 			var length = arr.length;
 			var newArr = [];
 			for(var i=0;i<length;i++){
     
     
 				var str = arr[i] * Math.pow(10,length-i-1);
 				// 将非0的数字放入到新的数组中
 				if(str !=0){
     
     
 					newArr.push(str);
 				}
 			}
 			// 将新数组中的数据通过+连接
 			// console.log(newArr.join(' + '));
 			return newArr.join(' + ');
 		}
 				
		// 验证
		console.log(expandedForm(12)); // Should return '10 + 2'
		console.log(expandedForm(42)); // Should return '40 + 2'
		console.log(expandedForm(70304)); // Should return '70000 + 300+4'
	</script>

【3】<7kyu>【String ends with?】

Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).

example

solution('abc', 'bc') // returns true
solution('abc', 'd') // returns false

solution:

<script type="text/javascript">
 		function solution(str, ending){
     
     
 			// 用endsWith判断字符串是否以谁结束
 			var temp = str.endsWith(ending);
 			return temp;
 		}
 				
		// 验证
		console.log(solution('abc', 'bc')); // true
		console.log(solution('abc', 'd')); // false
		console.log(solution('abcde', 'abc'))//false
	</script>

知识点

  • startsWith(anotherString)
    • 判断是否以为anotherString开头
  • endsWith(anotherString)
    • 判断是否以anotherString为结尾
"abcd".startsWith("ab"); // true
"abcd".startsWith("bc"); // false
"abcd".endsWith("cd");   // true
"abcd".endsWith("e");    // false
"a".startsWith("a");     // true
"a".endsWith("a");       // true

【4】<6kyu>【Delete occurrences of an element if it occurs more than n times】

Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].

example

deleteNth ([1,1,1,1],2) // return [1,1]

deleteNth ([20,37,20,21],1) // return [20,37,21]

solution:

<script type="text/javascript">
 		function deleteNth(arr,n){
     
     
 			// 判断数组是否为空
 			if(!arr){
     
     return null;}
 			// 判断当n<1时,则说明1次都不能出现,所以要返回[]
 			if(n < 1){
     
     return [];}

 			var result = [];
 			var itemCounts = {
     
     };
 			// 遍历数组 判断每个元素出现的次数
 			for(var i=0;i<arr.length;i++){
     
     
 				var item = arr[i];
 				var count = itemCounts[item] ||0;
 				// 如果出现的次数<n,就将该元素放入新的数组
 				if(count < n){
     
     
 					result.push(item);
 					itemCounts[item] = count+1;
 				}
 			}
 			// console.log(result);
 			return result;

 		}				
		// 验证
		console.log(deleteNth ([1,1,1,1],2)); // [1,1]
		console.log(deleteNth ([20,37,20,21],1)); //[20,37,21]
	</script>

【5】<6kyu>【Vasya - Clerk】

The new “Avengers” movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollar bill. An “Avengers” ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to every person and give change if he initially has no money and sells the tickets strictly in the order people queue?

Return YES, if Vasya can sell a ticket to every person and give change with the bills he has at hand at that moment. Otherwise return NO.

example

tickets([25, 25, 50]) // => YES 
tickets([25, 100]) // => NO. Vasya will not have enough money to give change to 100 dollars
tickets([25, 25, 50, 50, 100]) // => NO. Vasya will not have the right bills to give 75 dollars of change (you can't make two bills of 25 from one of 50)

思路

  • 如果是25,就收下,所以n25++;;
  • 如果是50,也收下,但要找25,所以n50+; n25–;;
  • 如果是100,收下,先找25,再判断n50是否大于0,如果还有50则优先找50,没有就找2张25,所以n25–; n50>0?n50–:n25-=2;;

solution:

<script type="text/javascript">
 		function tickets(peopleInLine){
     
     
 			// console.log(peopleInLine);
 			var len = peopleInLine.length;
 			var [n25,n50,n100] = [0,0,0];
 			for(let v of peopleInLine){
     
     
 				if(v == 25) n25++;
 				if(v == 50) {
     
     n50++; n25--;}
 				if(v == 100){
     
     n25--; n50>0?n50--:n25-=2;}
 				if(n25<0 || n50<0) return 'NO';
 				// console.log(n25,n50,n100);
 			}
 			return 'YES';			
 		}
 		
		// 验证
		console.log(tickets([25, 25, 50, 50])); // YES
		console.log(tickets([25, 100])); //NO
		console.log(tickets([25, 25, 50, 50, 100]));//NO

	</script>

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

猜你喜欢

转载自blog.csdn.net/FemaleHacker/article/details/113092600