codewars练习(javascript)-2021/2/19

本文介绍了四个JavaScript编程练习,包括数组元素加倍、混合类型数组处理、判断正数个数以及二维数组元素查找。通过解决这些问题,探讨了数组遍历、类型判断、条件判断等基本编程技巧。示例代码提供了详细的解决方案,并附有验证测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

codewars-js练习

2021/2/19

github 地址

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

【1】<8kyu>【Beginner - Lost Without a Map】

Given an array of integers, return a new array with each value doubled.

给定一个整数数组,返回一个新数组,每个值都加倍。

example

[1, 2, 3] --> [2, 4, 6]

solution

<script type="text/javascript">
    function maps(x){
      // console.log(x)
      var result = JSON.parse(JSON.stringify(x))
      for(var i=0;i<result.length;i++){
        result.splice(i,1,result[i]*2)
      }
      return result;
    }
 		
		// 验证
		console.log(maps([1,2,3]));//[2,4,6]
	</script>
【2】<7kyu>【Divide and Conquer】

Given a mixed array of number and string representations of integers, add up the string integers and subtract this from the total of the non-string integers.

给定一个由数字和字符串表示的整数混合数组,将字符串整数相加,然后从非字符串整数的总数中减去这个数。

example

[9, 3, '7', '3']// 2
['5', '0', 9, 3, 2, 1, '9', 6, 7]//14

solution

<script type="text/javascript">
    function divCon(x){
      console.log(x)
      var temp = [];
      var tempA = [];
      var sum = 0;
      var sumS = 0;
      for(var i=0;i<x.length;i++){
        if(typeof x[i] == 'string'){
          // console.log(x[i]);
          temp.push(parseInt(x[i]))
        }else{
          tempA.push(x[i])
        }
      }
      // console.log(temp)
      for(var j=0;j<temp.length;j++){
        sum += temp[j];
      }
      for(var z=0;z<tempA.length;z++){
        sumS += tempA[z];
      }
      return sumS - sum;
    }
 		
		// 验证
		console.log(divCon([9,3,'7','3']));//2
    console.log(divCon(['5', '0', 9, 3, 2, 1, '9', 6, 7]));//14
	</script>
【3】<7kyu>【Two numbers are positive】

Your job is to wite a function, which takes three integers a, b, and c as arguments, and returns True if exactly two of of the three integers are positive numbers, and False - otherwise.

它以三个整数a、b和c作为参数,如果三个整数中恰好有两个是正数则返回True,否则返回False。

example

twoArePositive(2, 4, -3) == true
twoArePositive(-4, 6, 8) == true
twoArePositive(4, -6, 9) == true
twoArePositive(-4, 6, 0) == false
twoArePositive(4, 6, 10) == false
twoArePositive(-14, -3, -4) == false

solution

<script type="text/javascript">
    function twoArePositive(a, b, c) {
      // console.log(a,b,c)
      var arr = [a,b,c];
      // console.log(arr)
      var num = 0
      for(var i=0;i<arr.length;i++){
        if(arr[i]>0)num ++;
      }
      if(num == 2)return true;
      return false;
    }
 		
		// 验证
		console.log(twoArePositive(2,4,-3));//true
    console.log(twoArePositive(-4,6,8));//true
    console.log(twoArePositive(-14,-3,-4));//false
	</script>
【4】<7kyu>【Clean up after your dog】

Given a 2D array to represent your garden, you must find and collect all of the dog cr@p - represented by ‘@’.

If you do, return ‘Clean’, else return ‘Cr@p’.

给定一个2D数组来代表你的花园,你必须找到并收集所有的狗cr@p -代表’@’。

example

x=
[[_,_,_,_,_,_]
[_,_,_,_,@,_]
[@,_,_,_,_,_]]

bags = 2, cap = 2

return --> 'Clean'

solution

<script type="text/javascript">
    function crap(x, bags, cap){
      // console.log(x,bags,cap)
      var numA = 0
      var numD = 0

      for(var i=0;i<x.length;i++){
        for(var j=0;j<x[i].length;j++)
          // console.log(x[i][j] == '@')
          if(x[i][j] == '@')numA++;
          else if(x[i][j] == 'D') numD ++;
      }
      // console.log(numA,numD)
      if(numD !=0)return 'Dog!!'
      else if(numA >cap)return 'Cr@p';
      else if(numA <= cap) return 'Clean'
    }
 		
		// 验证
		console.log(crap([['_','_','_','_'], ['_','_','_','@'], ['_','_','@', '_']], 2, 2));// "Clean"
    console.log(crap([['_','_','_','_'], ['_','_','_','@'], ['_','_','@', '_']], 1, 1));// "Cr@p"
    console.log(crap([['_','_'], ['_','@'], ['D','_']], 2, 2));// "Dog!!"
	</script>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值