簡體   English   中英

為什么不能在node.js中將此變量重新分配給新值?

[英]Why cant I reassign this variable to a new value in node.js?

我知道選擇排序算法不正確。 目前還可以,但是為什么不能在j for循環內將iMinimum分配給j? 我正在使用節點版本10+。 console.log“ iMinimum現在……”顯示了上面前面分配的變量的值,所以我很困惑。

 console.log('SELECTION SORT')

let data = [5,1,3,9,4]

for(let i = 0; i < data.length; i++){
 iMinimum = i


for(let j = iMinimum+1; j < data.length; j++){
     console.log(`j is now ${j}`)
     if(data[j] < data[iMinimum]){
         iMininum = j
         console.log(`iMinimum is now ${iMinimum} and j is ${j}`)
     }
}
data[i] = data[iMinimum] 
}

console.log(`Sorted Array: ${data}`)

您有一些錯別字,請注意iMininumiMinimum

要讓解釋器警告您未聲明的變量,請使用以下命令啟動文件

'use strict';

這將啟用嚴格模式 ,強制您聲明所有變量。

為了進行進一步的分析,請運行諸如eslint類的eslint 這將自動檢測您的問題:

   7:5   error  'iMinimum' is not defined                       no-undef
   8:17  error  'iMinimum' is not defined                       no-undef
  10:28  error  'iMinimum' is not defined                       no-undef
  11:14  error  'iMininum' is not defined                       no-undef
  12:45  error  'iMinimum' is not defined                       no-undef
  15:20  error  'iMinimum' is not defined                       no-undef

暫無
暫無

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

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