返回課程

將數字屬性值乘以 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;
    }
  }
}

在沙箱中開啟包含測試的解決方案。