2016-04-06 9 views
0

私はdo/whileループと論理演算子をjs内に入れようとしています。私の問題は、if/else if/elseステートメントをdoセクションに埋め込み、アラートをループし続けるときです。また、珍しい、中程度の、あるいはうまくいったもの以外のものを入力した場合は、else if文の代わりにelse if文を使用します。どこが間違っていますか?どんな助けもありがとう。do/while文のelseif文が無限ループのjavacript

ありがとうございました。あなたは個別にそれぞれの比較を行う必要があり

var food; 
var steak; 

food = prompt("Hey what type of food do you want to eat? We have steak,     chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase(); 

switch (food) { 
    case 'STEAK': 
     alert ("Sure no problem, how would you like your steak done?"); 
     steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase(); 
      do { 
       if (steak === "RARE") { 
        alert ("You eat the rare steak"); 
       } 
       else if (steak === "MEDIUM"||"WELL-DONE") { 
        alert("You eat your steak and it's good."); 
       } 
       else { 
        alert("Sorry didn't catch that."); 
        steak = prompt("Choose rare, medium or well-done."); 
       } 
      } while (steak !== "RARE" || "MEDIUM" || "WELL-DONE"); 
     break; 
} 
+3

のような構文if(ステーキ=== "MEDIUM" || "WELL-DONE") 'はJavaScriptで飛ばない。それぞれの比較は個別に行う必要があります。例えば。 'if(steak ===" MEDIUM "|| steak ===" WE​​LL-DONE ")' – j08691

+0

@ j08691は、比較の必要性を既に指摘しています。無限ループの理由は、 ''任意の文字列 ''は真実である、すなわち。比較は 'while(steak!==" RARE "|| true || true)と同じです。これは常に真です。 – vesse

答えて

0

あなたが 'while'構文で持っている条件が常に「true」に等しいので、無限ループに入ります。次のように

は何を持っていることである。また、

while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE") 

while (steak !== "RARE" || "MEDIUM" || "WELL-DONE") 

変更それは、あなたが「他の場合は、」文のいずれかで同様の問題を持っています。

else if (steak === "MEDIUM"|| steak === "WELL-DONE") { 

else if (steak === "MEDIUM"||"WELL-DONE") { 

変更それはまだあなたのスクリプト内のいくつかの論理的な問題があります。次のようにしてください:

var food; 
var steak; 

food = prompt("Hey what type of food do you want to eat? We have steak,     chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase(); 

switch (food) { 
case 'STEAK': 
    alert ("Sure no problem, how would you like your steak done?"); 

    steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase(); 
    while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE") { 
     alert("Sorry didn't catch that."); 
     steak = prompt("Choose rare, medium or well-done.").toUpperCase(); 
    } 

    if (steak === "RARE") { 
     alert ("You eat the rare steak"); 
    } 
    else if (steak === "MEDIUM"|| steak === "WELL-DONE") { 
     alert("You eat your steak and it's good."); 
    }    
    break; 
} 

まだ動作しない場合は教えてください。

0

、そう

else if (steak === "MEDIUM"||"WELL-DONE") { 
    alert("You eat your steak and it's good."); 
} 

はそうで

else if (steak === "MEDIUM"|| steak === "WELL-DONE") { 
    alert("You eat your steak and it's good."); 
} 

こととすべきです。 これは、無限のdo/whileループの原因でもあります。

0

としては

条件は(== N || == mの)場合は、別途
すなわちテストする必要があり、上記の指摘もあなたの最後の文が正しくありません。あなたは私のために働いた

var food; 
 
var steak; 
 

 
food = prompt("Hey what type of food do you want to eat? We have steak,     chicken, legumes, kangaroo or pufferfish?","Select your choice here.").toUpperCase(); 
 

 
switch (food) { 
 
    case 'STEAK': 
 
     alert ("Sure no problem, how would you like your steak done?"); 
 
     steak = prompt("Would you like your steak done rare, medium or well-done?").toUpperCase(); 
 
     console.log('steak', steak); 
 
      do { 
 
       console.log('steak inside do', steak); 
 
       if (steak === "RARE") { 
 
        alert ("You eat the rare steak"); 
 
       } 
 
       else if (steak === "MEDIUM"|| steak ==="WELL-DONE") { 
 
        alert("You eat your steak and it's good."); 
 
       } 
 
       else { 
 
        alert("Sorry didn't catch that."); 
 
        steak = prompt("Choose rare, medium or well-done.").toUpperCase(); 
 
       } 
 
      } while (steak !== "RARE" && steak !== "MEDIUM" && steak !== "WELL-DONE"); 
 
     break; 
 
}

ような何かをしたい場合があります。