附上相關連結
https://www.codewars.com/kata/515e271a311df0350d00000f/train/javascript
function squareSum(num){
let newNum = num.map(x => x*x);
console.log(newNum);
if(newNum.length>0){
return newNum.reduce((prev,curr)=>prev+curr);
}
}
顯示結果為
Basic Tests
Should pass sample tests
Log
[ 1, 4 ]
[ 1, 4 ]
[ 0, 9, 16, 25 ]
[]
expected undefined to equal 0
我不知道如何處理這個問題,以及為什麼,有人可以幫助我一下嗎?
function squareSum(numbers){
let newNum = numbers.map(x => x*x);
if (newNum.length > 0) {
return newNum.reduce((prev,curr)=>prev+curr);
} else {
return 0;
}
}