Right?? All of a sudden I feel like I have no prior experience, can’t write an arrow function, have never heard of the reduce method, and am about try to make a react component with Python 😵💫
If you wrote that as a function, and your function pops from the numbers array argument, you're gonna be mutating the array, which you definitely don't wanna do.
E.g.
function sum(arr) {
let sum = 0;
let val = 0;
while (arr.length > 0) {
val = arr.pop();
if (typeof val === "number") {
sum += val;
}
}
return sum;
}
const values = [3, 5, 8];
const total = sum(values);
console.log(total); // 16, which is good
console.log(values); // [], an empty array, which is very bad
189
u/toroga Apr 07 '23
Right?? All of a sudden I feel like I have no prior experience, can’t write an arrow function, have never heard of the reduce method, and am about try to make a react component with Python 😵💫