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

codewars-js练习

2021/1/26

github 地址

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

【1】<8kyu>【Dollars and Cents】

you have volunteered to create a function that will take a float and return the amount formatting in dollars and cents.

39.99 becomes $39.99

example

3 needs to become $3.00

3.1 needs to become $3.10

solution

<script type="text/javascript">
 		function formatMoney(amount){
      var count = String(amount).length - (String(amount).indexOf(".") + 				1);
      if(amount % 1 ==0){
          return '$'+amount +'.00';
      }else if(count == 1){
          return '$'+amount +'0';
      }
  return '$'+amount;
 		}
 		
		// 验证
		console.log(formatMoney(39.99));// '$39.99'
	</script>
【2】<6kyu>【Mexican Wave】

In this simple Kata your task is to create a function that turns a string into a Mexican Wave. You will be passed a string and you must return that string in an array where an uppercase letter is a person standing up.

  1. The input string will always be lower case but maybe empty.

  2. If the character in the string is whitespace then pass over it as if it was an empty seat

example

wave("hello") => ["Hello", "hEllo", "heLlo", "helLo", "hellO"]

solution

<script type="text/javascript">
 		function wave(str){
 			var arr = str.toLowerCase().split('');
 			// console.log(arr);
 			var result = [];
 			for(var i=0;i<arr.length;i++){
 				var temp = str.substring(0,i) + arr[i].toUpperCase() + str.substring(i+1);
 				if(temp != str){
 					result.push(temp);
 				}
 			}
 			// console.log(result);
 			return result;
 		}
 		
		// 验证
		console.log(wave("hello"));// "Hello", "hEllo", "heLlo", "helLo", "hellO"
		console.log(wave("gap"));// "Gap", "gAp", "gaP"
		console.log(wave("two words"));//"Two words", "tWo words", "twO words", "two Words", "two wOrds", "two woRds", "two worDs", "two wordS";
	</script>
【3】<6kyu>【Break camelCase】

Complete the solution so that the function will break up camel casing, using a space between words.

example

solution("camelCasing")  ==  "camel Casing"

solution

<script type="text/javascript">
		function solution(string) {
			// string字符串分隔而得到的数组
			var arr = string.split('');
			// 全部大写之后得到的数组
			var strArr = string.toUpperCase().split('');
			var temp = [];
			// 获取到大写字母处的index
			for(var i=0;i<arr.length;i++){
				if(arr[i] == strArr[i]){
					temp.push(i);
					// console.log(temp);
				}
			}
			// 找到位置,插入空格
			var count = 0;
			for(var j=0;j<temp.length;j++){
				// 需要插入空格位置的index坐标
				// console.log(temp[j]+count);
				// splice删除元素,并添加新元素。
				// splice(index,删除数量,新元素)
				arr.splice(temp[j]+count,0,' ');
				count ++;
			}
			return arr.join('');
		}
 		
		// 验证
		console.log(solution("camelCasing"));//"camel Casing"
		console.log(solution('camelCasingTest'));// 'camel Casing Test', 'Unexpected result'
	</script>
【4】<7kyu>【Factorial】

In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example: 5! = 5 * 4 * 3 * 2 * 1 = 120. By convention the value of 0! is 1.

Write a function to calculate factorial for a given input. If input is below 0 or above 12 throw a RangeError (JavaScript) .

求阶乘

example

factorial(0)// 1
factorial(1)//1
factorial(2)// 2
factorial(3)// 6

solution

<script type="text/javascript">
 			function factorial(n){
//  			console.log('n',n);
 			var i =1;
 			var sum =1;
 				
 				if(n>1 && n<=12){
 					while (i <= n) {
        				sum *= i;
        				i++;
    				}
 					return sum;
 				}else if(n == 0 || n == 1){
 					return 1;
 				}else{throw new RangeError();}
 			}
 		
		// 验证
		console.log(factorial(0));//1
		console.log(factorial(1));//1
		console.log(factorial(5));//120
		console.log(factorial(13));//报错
	</script>
【5】<7kyu>【Categorize New Member】

To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.

Input will consist of a list of lists containing two items each. Each list contains information for a single potential member. Information consists of an integer for the person’s age and an integer for the person’s handicap.

Note for F#: The input will be of (int list list) which is a List

[age,handicap],只有当age>=55 并且handicap>7时才为senior,否则为open

example

[[18, 20],[45, 2],[61, 12],[37, 6],[21, 21],[78, 9]]//["Open", "Open", "Senior", "Open", "Open", "Senior"]

solution

<script type="text/javascript">
 		function openOrSenior(data){
 			var str = '';
 			var result = [];
 			for(var i=0;i<data.length;i++){
 				//判断是否符合senior条件
 				if(data[i][0]>=55 && data[i][1]>7){
 					str = 'Senior';
 					result.push(str);
 				}else{
 					str = 'Open';
 					result.push(str);
 				}
 			}
 			// console.log(result);
 			return result;
 		}
 		
		// 验证
		console.log(openOrSenior([[45, 12],[55,21],[19, -2],[104, 20]]));//['Open', 'Senior', 'Open', 'Senior'])
		console.log(openOrSenior([[3, 12],[55,1],[91, -2],[54, 23]]));//['Open', 'Open', 'Open', 'Open']
		console.log(openOrSenior([[59, 12],[55,-1],[12, -2],[12, 12]]));//['Senior', 'Open', 'Open', 'Open'])

	</script>

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值