2015-12-17 63 views
2

問題是重複的,抱歉。使用JS的陣列問題

+3

它看起來像你想'if(index [0] === 1)'等等。 – JJJ

+0

爲什麼對所有答案都如此贊同? – Rokin

+0

@Rraham請解釋一下。 – Dropout

回答

2

您正在嵌套您的if陳述。將它們設置在同一級別上。也可以使用if else

if (index == 1) { 
    console.log('Thank you, your Drink' + ' has now been dispensed.'); 
} 
else if (index == 2) { 
    console.log('Thank you, your Crisps' + ' has now been dispensed.'); 
} 
else if (index == 3) { 
    console.log('Thank you, your Chocolate' + ' has now been dispensed.'); 
} 
else if (index == 4) { 
    console.log('Thank you, your Candy' + ' has now been dispensed.'); 
} 
+0

謝謝你的反饋! – ZenoX

2

你的大括號會混亂。你讓他們一個在另一個這樣的:

if(){ 
    if(){ 
     if(){ 
      if(){ 
      } 
     } 
    } 
} 

試着改變你的代碼:

var readlineSync = require('readline-sync'), 
products = []; 
products[1] = "Drink"; 
products[2] = "Crisps"; 
products[3] = "Chocolate"; 
products[4] = "Candy"; 

var productPurchase = readlineSync.question('Would you like to purchase a product? '); 
if (productPurchase == "yes") { 
    index = readlineSync.keyInSelect(products, 'What product would you like?'); 
    if (index == 1) { 
     console.log('Thank you, your Drink' + ' has now been dispensed.'); 
    } 
    if (index == 2) { 
     console.log('Thank you, your Crisps' + ' has now been dispensed.'); 
    } 
    if (index == 3) { 
     console.log('Thank you, your Chocolate' + ' has now been dispensed.'); 
    } 
    if (index == 4) { 
     console.log('Thank you, your Candy' + ' has now been dispensed.'); 
    } 

} 

使用正確的縮進有助於避免類似這樣的問題。如果你的代碼寫得更乾淨,你可能會馬上注意到這個問題。

注意:您可能希望將這些if語句更改爲switch。這可能是最好的情況。

+0

謝謝,我會考慮在這種情況下使用開關! :) – ZenoX

3

試試這個:

var readlineSync = require('readline-sync'), 
products = []; 
products[1] = "Drink"; 
products[2] = "Crisps"; 
products[3] = "Chocolate"; 
products[4] = "Candy"; 

var productPurchase = readlineSync.question('Would you like to purchase a product? '); 
if (productPurchase == "yes") { 
    index = readlineSync.keyInSelect(products, 'What product would you like?'); 
    if (index == [1]) 
     console.log('Thank you, your Drink' + ' has now been dispensed.'); 
    else if (index == [2]) 
     console.log('Thank you, your Crisps' + ' has now been dispensed.'); 
    else if (index == [3]) 
     console.log('Thank you, your Chocolate' + ' has now been dispensed.'); 
    else if (index == [4]) 
     console.log('Thank you, your Candy' + ' has now been dispensed.'); 
} 
+0

謝謝您的反饋! – ZenoX

5

你嵌套if的永遠不會被執行,index不能同時爲1和3在同一時間。我建議你使用switch相反,它基本上是什麼你正在嘗試做的:

var productPurchase = readlineSync.question('Would you like to purchase a product? '); 
if (productPurchase == "yes") { 
    index = readlineSync.keyInSelect(products, 'What product would you like?'); 
    switch (index) { 
     case 1 : 
     console.log('Thank you, your Drink' + ' has now been dispensed.'); 
     break; 
     case 2 : 
     console.log('Thank you, your Crisps' + ' has now been dispensed.'); 
     break; 
     case 3 : 
     console.log('Thank you, your Chocolate' + ' has now been dispensed.'); 
     break; 
     case 4 : 
     console.log('Thank you, your Candy' + ' has now been dispensed.'); 
     break; 
     default : 
     console.log('something went wrong'); 
     break; 
    } 
} 

注:已經糾正的index錯誤的用法。 keyInSelect()將索引作爲數字返回,而不是數組。

+1

爲什麼不放棄'switch'並將其替換爲'console.log('謝謝,您的'+產品[index] +'現在已經被分配。');' – Marc

+0

謝謝我將研究在這種情況下使用開關然後! :) – ZenoX

+0

這個答案是正確的。在語句中去掉「==」並僅使用「===」也是一個好主意,因爲JavaScript非嚴格比較運算符「==」通常會顯示不可預知的行爲。它會在這裏正常工作,但這是不好的做法。 –