Please, help to undestand the problem? Can’t het result – total amount
const objectWithNumbers = {
a: 10,
b: 20,
c: "string",
d: 12,
};
function sumObjectValues(obj) {
Object.values(obj).reduce((sum, key) => {
if (typeof obj[key] === "number") {
sum += obj[key];
}
return sum
}, 0);
}
const result = sumObjectValues(objectWithNumbers);
console.log(result);
//42should be
Tried to use reduce and can’t get result
2
Answers
few errors.
Object.values
actually gives the values not the keysor you can use
Object.keys
An one-liner with a ternary operator: