簡體   English   中英

在一個數字序列中,如何計算一個數字出現多少次,該值恰好比前一個數字的值小一?

[英]How does one, within a sequence of digits, count how many times a digit appears thats value is exactly one less than the previous digit's one?

代碼:

 function OneDecremented(num) { num = num.toString() var count = 0 for(i = 1; i < num.length; i++) { if(num[i - 1] - num[i] === 1){ count++ } } return count } console.log(OneDecremented(9876541110))

所以我很難理解兩件事:

  1. i 和 num[i] 有什么區別
  2. 我不明白 if 語句中的計算是如何發生的,有人可以分解嗎?

抱歉,如果這些問題聽起來太傻,我是 JS 新手,無法真正理解算術計算。 謝謝你的時間。

這段代碼寫得不好有幾個原因,但最重要的是,它在全局范圍內泄露了i引用,所以,讓我們從一個更好的版本開始:

function OneDecremented(num) {
  var str = num.toString();
  var count = 0;
  for(var i = 1; i < str.length; i++) {
    if(str[i - 1] - str[i] === 1)
      count++;
  }
  return count;
}

在現代 JS 中,字符串可以像 arrays 一樣訪問,索引返回index position 處的字符:

if(str[i - 1] - str[i] === 1)
// is the same as
if ((str.charAt(i - 1) - str.charAt(i)) === 1)

一旦檢索到每個字符,代碼就會進行隱式的“字符到數字”轉換,這要歸功於-運算符,但如果它是一個+ ,它會將兩個字符連接為字符串(所以,要小心)。

明確一點總是更好,但如果你知道-是如何工作的,它就可以完成這項任務。

循環從1開始,它檢查i - 1處的 char (在第一次迭代中,索引0處的 char 減去當前 char )是1 ,這意味着當前 char比前一個少一個

在這種情況下,計數器會累加。

i 和 num[i] 有什么區別

i是迭代鍵,即 0、1、2 等,因為字符串化數字中有盡可能多的字符。 num[i]是字符串中索引i處的字符,即num[i] ,其中i為 0 == 9 (字符串中索引為 0 處的字符)。

我不明白 if 語句中的計算是如何發生的,有人可以分解嗎?

也就是說:如果計算字符串索引i-1處的數字減去當前考慮的數字(字符串中的索引i處)減去 1,則增加count

逐步使用的實際數量:

  • 9 - 沒有前一個字符; 計算(未定義 - 9)不等於 1
  • 8 - 前一個字符是 9; (9 - 8) == 1; 遞增count
  • 7 - 同上
  • 6 - 同上
  • 5 - 同上
  • 4 - 同上
  • 1 - 前一個字符是 4; 計算 (4 - 1) 不等於 1
  • 1 - 前一個字符為 1; 計算 (1 - 1) 不等於 1
  • 1 - 同上
  • 0 - 前一個字符為 1; (1 - 0) == 1; 遞增count

Andrea 和 Mitya 已經成功了。

下一步可能是切換到第一個基於 class的方法,例如使用特定的Array方法,例如reduce

如果實施正確,這種方法通常會提高代碼的可讀性/可維護性,並允許更好的代碼重用。

對於 OP 提供的示例,可以編寫兩個函數,即獲取計數的實際方法和上述第一個 class 減速器功能。 由於reduce是如何處理arrays的標准方法,因此reducer/callback的參數優先級也得到了很好的指定......

[/* ... */].reduce(function(accumulator, currentValue, currentIndex, currentlyProcessedArray) {

  // implement reducer/aggregation/accumulator logic here.

  // the return value serves as the
  // new `accumulator` value within
  // the next iteration step.

  // thus, always return something! ... e.g ...

  return (accumulator + currentValue); 
});

 function aggregatePrecursorAndDecrementedSuccessorCount(count, char, idx, arr) { const precursorValue = Number(arr[idx - 1]); const incrementedCurrentValue = (Number(char) + 1); const isValidCount = (precursorValue === incrementedCurrentValue); return (count + (isValidCount? 1: 0)); //return (count + Number(isValidCount)); // cast boolean value to either 1 or 0. } function getPrecursorAndDecrementedSuccessorCount(int) { return String(int) // - assure/typecast always a/into string value. .split('') // - split string value into an array of single characters. .reduce(aggregatePrecursorAndDecrementedSuccessorCount, 0); } console.log(getPrecursorAndDecrementedSuccessorCount(9876541110));
 .as-console-wrapper { min-height: 100%;important: top; 0; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM