I did this challenge a long time ago (I am a compSci student that was busy with other languages) and now I can’t find my solutions any more after the site has been redesigned. That is not cool!
Anyway, I am stuck on the “Removing Items Using Splice()” challenge. I did this:
function sumOfTen(arr) {
// change code below this line
for (var i = 0; i < arr.length; i++) {
if (arr[i] + arr[i+1] === 10) {
arr.splice(arr[i+1], arr.length);
}
}
// 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]));
Anyway, I am sure my splice call is wrong. I have been looking everywhere for the solution but I do not know what I am doing wrong. Any help is appreciated. Thanks!
You have to remove elements using splice so that when you use the reduce method in the challenge, it sums up all the elements in the array and the result is 10. No loop is needed, just remove stuff from array
Just for reference sake, the splice() function syntax takes an array and removes the elements in the array from the specified starting index to the desired number to be removed:
arr.splice(indexToStartRemovingFrom, howManyItemsShouldIRemove);
it can also take some optional arguments of “whatShouldIReplaceTheseItemsWith”. that is beyond the scope of the task.
To the task itself, use the splice function to remove index[1] and index[2]. the remaining elements would give you a sum of 10. see the solution below
function sumOfTen(arr) {
// change code below this line
arr.splice(1,2);
// 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]));
Why do we take two numbers started from the first ("arr.splice(1, 2)) ? It will be the number 5 and number 1 and the summ of these two numbers couldnt give us 10. Or I dont understand how reduce function works?
Thanks))
to get a sum of 10 from the array above, we need to remove 2 elements (5 and 1). As i mentioned in my earlier post, the splice function woks like this: arr.splice(indexToStartRemovingFrom, howManyItemsShouldIRemove);
to remove “5 and 1” we start from 5, which is in index[1], and remove 2 elements (index[1] and index[2]) .
I found a different solution than what others did. My solution involved using splice multiple times. First I use shift, then I used slice, and then I used slice again. The arr array was [5, 5] by the end of that process. I hadn’t observed that you could just remove one 5 and then remaining numbers would sum to 10.
When I started doing this question, the array changed to [2, 4, 5, 1, 7, 5, 2, 1].
I came to this for my solution:
// only change code below this line
let sum = 0;
for (let i = 0; i < arr.length; i++) {
if (sum + arr[i] <= 10) {
sum += arr[i];
} else {
arr.splice(i, 1, 0);
}
}
// only change code above this line
I have been wrangling with a general solution to this challenge and couldn’t find an answer. After some time, I took inspiration from your code and implemented something pretty much identical.
The problem I have here is, that if we set the last item in arr to be 22, the sum will not be equal to 10…
After all we are only adding the numbers from left to right.
If you have a question about a specific challenge as it relates to your written code for that challenge, just click the Ask for Help button located on the challenge. It will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.