2016-01-17 137 views
0

該程序將提供三種路線選擇,然後詢問有關裝備,鞋子和寵物的信息。在全部選中後,我使用switch()聲明根據用戶的選擇給出相應的答案。Codecademy上的JavaScript控制流程編程

問題是響應始終是我對if()條件的響應,這意味着如果條件未滿足,它仍會記錄相同的響應。

這是Code Academy的練習;該網站說我已經完成了該計劃的要求,但當然,由於結果不對,我想我會尋求幫助。感謝所有提前幫助的人。 :)

var user = prompt("Your path diverges to three roads, which one will you take?", "Forest, Mountain or Sea?").toUpperCase(); 
var gear = ["Oxygen tank", "Fire Starter", "Camp"] 
var askGear = prompt("Choose your gear:", gear[0] + ", " + gear[1] + " or " + gear[2]).toUpperCase(); 
var shoes = ["Swimfin", "Studded", "BearPaw"] 
var askShoes = prompt("Choose your shoes:", shoes[0] + ", " + shoes[1] + " or " + shoes[2]).toUpperCase(); 
var pet = ["Monkey", "Wolf", "Whale"] 
var askPet = prompt("Choose your pet:", pet[0] + ", " + pet[1] + " or " + pet[2]).toUpperCase(); 
var choiceBank = [askGear, askShoes, askPet] 
var choices = confirm("You chose to take " + askShoes + " shoes, " + askGear + " and a " + askPet + " to your adventure in the " + user + ". Are you ready to go?") 
var choiceConfirm = confirm("This is your final chance, you sure?") 

switch(user) { 
    case "FOREST": { 
     if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', choiceBank[1] = 'BEARPAW', choiceBank[2] = 'WOLF' || 'MONKEY') { 
      console.log("Congratulations! With those right supplies you chose, you survived to live another day!") 
     } else { 
      console.log("Really? You though you could survive with those supplies?") 
     } 
    } 
     break; 
    case "MOUNTAIN": { 
     if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', choiceBank[1] = 'STUDDED', choiceBank[2] = 'WOLF') { 
      console.log("Wow! You're a survival expert!") 
     } else { 
      console.log("Really? You though you could survive with those supplies?") 
     } 
    } 
     break; 
    case "SEA": { 
     if (choiceBank[0] = 'OXYGEN TANK', choiceBank[1] = 'SWIMFIN', choiceBank[2] = 'WHALE') { 
      console.log("Congratulations on choosing well, you survived to live another day!") 
     } else { 
      console.log("Really? You though you could survive with those supplies?") 
     } 
    } 
     break; 
    default: { 
     console.log("Sorry, one of the responses was invalid, please try again.") 
     } 
} 
+1

你的'如果(choiceBank [0] = 'CAMP' || 'FIRE起動機',choiceBank [1] ='BEARPAW',choiceBank [2] ='WOLF'||'MONKEY'){'有意義。逗號''應該做些什麼?這個任務應該做什麼,或者'||'應該做什麼? –

+0

逗號應該添加另一個條件,如果選擇了任何一個元素,則該條件應該使條件成立。 –

+1

但這不是JavaScript。並且是'&&',優先級在前或'||',所以任何或者必須在括號中。 –

回答

2

發生了什麼變化:

  • var塊,現在緊湊,分離
  • statements;
  • case塊分離不需要多餘的{}
  • if改變到一些更有意義的。

原始代碼

if (choiceBank[0] = 'CAMP' || 'FIRE STARTER', 
    choiceBank[1] = 'BEARPAW', 
    choiceBank[2] = 'WOLF' || 'MONKEY') { 
//    ^ ^^ 
//  assignment  or and comma 

改爲

if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && 
    choiceBank[1] === 'BEARPAW' && 
    (choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) { 

choiceBank[0] = 'CAMP'是賦值,但我們需要choiceBank[0] === 'CAMP''FIRE STARTER'相同的比較。這應該是'CAMP'選擇的替代選擇。這是通過logical or||來實現的。 ,被替換爲&&。儘管的優先級小於,但需要括號。

  • 微小的變化:增加了用於輸出的,而不是console.log

var user = prompt("Your path diverges to three roads, which one will you take?", "Forest, Mountain or Sea?").toUpperCase(), 
 
    gear = ["Oxygen tank", "Fire Starter", "Camp"], 
 
    askGear = prompt("Choose your gear:", gear[0] + ", " + gear[1] + " or " + gear[2]).toUpperCase(), 
 
    shoes = ["Swimfin", "Studded", "BearPaw"], 
 
    askShoes = prompt("Choose your shoes:", shoes[0] + ", " + shoes[1] + " or " + shoes[2]).toUpperCase(), 
 
    pet = ["Monkey", "Wolf", "Whale"], 
 
    askPet = prompt("Choose your pet:", pet[0] + ", " + pet[1] + " or " + pet[2]).toUpperCase(), 
 
    choiceBank = [askGear, askShoes, askPet], 
 
    choices = confirm("You chose to take " + askShoes + " shoes, " + askGear + " and a " + askPet + " to your adventure in the " + user + ". Are you ready to go?"), 
 
    choiceConfirm = confirm("This is your final chance, you sure?"); 
 

 
switch (user) { 
 
    case "FOREST": 
 
     if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'BEARPAW' && (choiceBank[2] === 'WOLF' || choiceBank[2] === 'MONKEY')) { 
 
      out("Congratulations! With those right supplies you chose, you survived to live another day!"); 
 
     } else { 
 
      out("Really? You though you could survive with those supplies?"); 
 
     } 
 
     break; 
 
    case "MOUNTAIN": 
 
     if ((choiceBank[0] === 'CAMP' || choiceBank[0] === 'FIRE STARTER') && choiceBank[1] === 'STUDDED' && choiceBank[2] === 'WOLF') { 
 
      out("Wow! You're a survival expert!"); 
 
     } else { 
 
      out("Really? You though you could survive with those supplies?"); 
 
     } 
 
     break; 
 
    case "SEA": 
 
     if (choiceBank[0] === 'OXYGEN TANK' && choiceBank[1] === 'SWIMFIN' && choiceBank[2] === 'WHALE') { 
 
      out("Congratulations on choosing well, you survived to live another day!"); 
 
     } else { 
 
      out("Really? You though you could survive with those supplies?"); 
 
     } 
 
     break; 
 
    default: 
 
     out("Sorry, one of the responses was invalid, please try again."); 
 
} 
 

 
function out(s) { 
 
    var node = document.createElement('div'); 
 
    node.innerHTML = s + '<br>'; 
 
    document.getElementById('out').appendChild(node); 
 
}
<div id="out"></div>

+0

建議添加一些解釋,說明原始的塊是否做了解釋,說明它們錯誤的原因。 – Krease

+0

@Chris,請參閱編輯。 –

+0

非常感謝Nina!還有一件事,你能幫我理解最後的「功能(s,pre)」塊嗎? 'pre'和'.appendChild(node)'是什麼? –