codewars-js练习
2021/2/17
github 地址
【1】<8kyu>【Beginner - Reduce but Grow】
Given a non-empty array of integers, return the result of multiplying the values together in order.
example:
[1, 2, 3, 4] => 1 * 2 * 3 * 4 = 24
solution
<script type="text/javascript">
function grow(x){
// console.log(x);
var result=1;
for(var i=0;i<x.length;i++){
result *=x[i];
}
return result;
}
// 验证
console.log(grow([1,2,3,4]));// 24
</script>
【2】<7kyu>【Spoonerize Me】
In its most basic form a spoonerism is a two word phrase in which only the first letters of each word are swapped:
“not picking” --> “pot nicking”
Your task is to create a function that takes a string of two words, separated by a space: words
and returns a spoonerism of those words in a string, as in the above example.
首字母互换
example:
"not picking" --> "pot nicking"
solution
<script type="text/javascript">
function spoonerize(words) {
// console.log(words)
var arr = words.split(' ');
// console.log(arr)
var arr1 =arr[0].split('')
var arr2 = arr[1].split('')
// console.log(arr1)
// console.log(arr2)
var temp1 = arr2[0]
var temp2 = arr1[0]
arr1.shift()
arr2.shift()
// console.log(arr2)
// console.log('arr1',arr1)
var result = temp1 + arr1.join('') +' '+ temp2 + arr2.join('')
// console.log('result',result)
return result
}
// 验证
console.log(spoonerize("nit picking"));//"pit nicking"
console.log(spoonerize("jelly beans"));// "belly jeans"
</script>
以上为自己思路供大家参考,可能有更优的思路。