You are given an object called sumMe containing several key/value pairs and a variable called total whose initial value is 0. Using a for... in loop, iterate through the keys of sumMe; if the value corresponding to a key is a number, add it to total
can someone please explain why I'm getting zero for my result? total should equal 15.
const sumMe = {
hello: 'there',
you: 8,
are: 7,
almost: '10',
done: '!'
};
let total = 0;
// iterate by using for...in loop
for (let i in sumMe) {
// corresponding to a key is a number by using object and value
if (typeof Object.values(sumMe) === 'number') {
// adding the results and total should equal 15.
total += Object.values(sumMe);
}
}
// print out
console.log(total);
Please make corrections when getting current values in the loop.
const sumMe = {
hello: 'there',
you: 8,
are: 7,
almost: '10',
done: '!'
};
let total = 0;
// iterate by using for...in loop
for (let i in sumMe) {
// corresponding to a key is a number by using object and value
if (typeof sumMe[i] === 'number') {
// adding the results and total should equal 15.
total += sumMe[i];
}
}
// print out
console.log(total);