need help with this code
Your code so far
function sumOfTen(arr) {
// change code below this line
let a = arr.splice(1,1);
let b = arr.splice(3,1);
// change code above this line
return arr.reduce((a, b) => a + b);
}
// do not change code below this line
console.log(sumOfTen([2, 5, 1, 5, 2, 1]));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/remove-items-using-splice
So the challenge is to remove enough numbers from the given array so that the final list sums up to 10.
Here’s the original list:
[2, 5, 1, 5, 2, 1]
And then here’s what happens when your code runs
let a = arr.splice(1,1); //remove one item from position index 1
a is
[5]
arr is now
[2,1,5,2,1]
let b = arr.splice(3,1); //remove one item from position index 3
b is
[2]
arr is also
[2,1,5,1]
return arr.reduce((a, b) => a + b);
This produces the sum of 2+1+5+1 = 9
Therefore the challenge is not met.
1 Like
ohh i thought i have to splice numbers from the array and assign those to a &b
oh oops, you’re right. The a and b above are the deleted items. I’ll edit that…
but to be clear, the ‘a’ and ‘b’ in the scope of the ‘reduce’ are completely different things than the ‘a’ and ‘b’ arrays you made…
De-Bee
July 20, 2018, 12:32pm
6
Following your logic, you could just splice from the 2nd item and then 2 items.
let a = arr.splice(1,2);
a would already add up to 10. You bet that you could just add up 0 by defining b as so…
can i used return value of arr.splice as parameter in function