将数值属性值乘以 2
重要性:3
创建一个函数 multiplyNumeric(obj),它将 obj 的所有数值属性值乘以 2。
例如
// before the call
let menu = {
width: 200,
height: 300,
title: "My menu"
};
multiplyNumeric(menu);
// after the call
menu = {
width: 400,
height: 600,
title: "My menu"
};
请注意,multiplyNumeric 不需要返回任何内容。它应该就地修改对象。
附注:使用 typeof 检查数字。
function multiplyNumeric(obj) {
for (let key in obj) {
if (typeof obj[key] == 'number') {
obj[key] *= 2;
}
}
}