亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

要求解釋代碼的某些部分

要求解釋代碼的某些部分

慕碼人8056858 2023-01-06 16:20:47
我得到了這個測試來解決它,我解決了它,測試要求返回數字的總和,這很容易返回它,但問題是返回它(如果數字是負數,第一個例如,數字應算作負數)。let output = sumDigits(1148);   console.log(output); // --> 14     let output = sumDigits(-316);   console.log(output); // --> 4就像我說的那樣解決const sumDigits = num => {  let ar = num.toString().split('')  //Stringify the num and convert it to an array  let minSum = 0 // initialize the minSum counter and set to the value of 0  let plsSum = 0 // initialize the plsSum counter and set to the value of 0  //checking if the array start with '-', and if it's i'm going to remove it.  if (ar[0] === '-') {    ar.splice(0, 1)    ar.reduce((a, b) => minSum = Math.abs(a - b)) // subtracting the arrray of numbers and convet it to number after removing the first char.  }  // iterate over the array.  for (let i = 0; i < ar.length; i++) {    // adding the sum of array numbers to the initial var and convert it to a number    plsSum += Number(ar[i])  }  //returning the minSum and plsSum  if (minSum) {    return minSum  } else {    return plsSum  }}let output = sumDigits(1148)console.log(output) // --> 14let output2 = sumDigits(-316)console.log(output2) // --> 4但是當我在搜索的時候,我發現這段代碼和reduce在一行中,有些代碼我看不懂,這就是我問你們的原因。這是代碼const sumDigits = num =>String(num).split('').reduce((a,v,idx,arr)=> v === '-' ? (v = 0, arr[idx+1] *= -1, a + +v) :a+ +v,0)所以讓我們分解一下。String(num).split('') 在這部分中,他們將其串起來并將其轉換為數組。?reduce((a,v,idx,arr) 在這部分中,他們用 4 個參數初始化 reduce。?v === '-' ?在這部分,他們檢查是否v等于'-',但問題是 在第一個輸出 (1148)v中從1 開始,在第二個輸出 (-316)中從 3 開始,因為將以1和' -'對吧?然后他們設置(v = 0)。然后它們乘以-1我 的問題是為什么? 如果有人不介意解釋其余代碼,我們將不勝感激。 感謝提前。aarr[idx+1] *= -1
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

您可以Math.abs在拆分之前使用數字并將其轉換為字符串,然后使用 reduce 來計算總和。在從函數返回之前檢查輸入是小于還是大于 0 并相應地采取措施


function sumDigits(num) {

  // toString will convert to string so an array of string can be created

  const sum = Math.abs(num).toString().split('').reduce((acc, curr) => {

    // converting string to number before adding with previous digit

    // else it will do string concatenation instead of mathematical addition

    acc += +curr;

    return acc

  }, 0);

  return num < 0 ? -1 * sum : sum;

}




let output = sumDigits(1148);

console.log(output); // --> 14  


let outpu2t = sumDigits(-316);

console.log(outpu2t); // --> -10


查看完整回答
反對 回復 2023-01-06
?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

我將重點關注 reduce 方法的部分。reduce Array 方法可以接收兩個參數,第一個表示將“減少”數組的回調,這個回調可以接收 4 個參數:

  1. 電池

  2. 當前值

  3. 當前指數

  4. 大批

reduce 方法的第二個參數指示哪個值將啟動回調的Acumulator參數。

一旦解釋說,在您看到的示例中,他表示累加器將從 0 值開始:

.reduce(<...>, 0)

然后,在 reduce 方法的第一次迭代中,當前值的第一個值將是數組的 0 索引值。

num如果我們考慮是的情況-316,那么:

第一次迭代:回調變量將是:


a = 0

v = '-'

idx = 0

arr = ['-', '3', '1', '6']

該過程將是:


v === '-' //true, then:

v = 0

arr[idx+1] *= -1 //here, he are converting the value next to the sign to a negative value

a + +v //then, he add the v value to the acumulator with the corresponding sign.

第二次迭代:回調變量


 a = 0

 v = -3

 idx = 1

 arr = ['-', -3, '1', '6']

過程:


v === '-' //false, then:

a + +v //a = 0, v = -3. 0 + +(-3) = -3 (Number)

我認為你可以貶低故事的其余部分。


查看完整回答
反對 回復 2023-01-06
?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

簡短回答:arr[idx+1] *= -1直接將數組中的下一個成員操作為負整數。


您可以在 Javascript Playground 上嘗試以下代碼,以查看每個循環步驟的變量值,以便更好地理解:(這是您試圖理解的代碼的擴展版本)


function sum(num) {

  s = String(num)

    .split('')

    .reduce(function (a, v, idx, arr) {

      console.log('a=', a, 'v=', v, 'idx=', idx, 'arr=', arr);

      if (v === '-') {

        v = 0;

        arr[idx + 1] *= -1;

        a += +v;

      } else {

        a += +v;

      }

      return a;

    }, 0);

  return s;

}

console.log(sum(1148));

console.log(sum(-316));


查看完整回答
反對 回復 2023-01-06
  • 3 回答
  • 0 關注
  • 208 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號