文章目录
codewars-js练习
2021/2/19
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>
以上为自己思路供大家参考,可能有更优的思路。