I have an object called sumMe containing several key/value pairs and a variable called total whose initial value is 0. I'm using a for... in loop to iterate through the keys of sumMe and if the value corresponding to a key is a number, I want to add it to total.
const sumMe = {
hello: 'there',
you: 8,
are: 7,
almost: '10',
done: '!'
};
let total = 0;
for (let keys in sumMe ) {
if (typeof(sumMe[keys]) = "number") {
total + sumMe[keys]
}
}
console.log(sumMe)
I get the following error when I try to evaluate it: Syntax Error: Invalid left-hand side in assignment
The expected total should equal 15. It currently reads "0" when correcting for the comparison operator changing "=" to "===" above.
The equality check is missing the second '='. And maybe you'd like to increase total, so change + to +=.
Write
for (let keys in sumMe ) {
if (typeof(sumMe[keys]) == "number") {
total += sumMe[keys]
}
}
hth