2015-10-26 3 views
0

Все работает нормально, но когда я пытаюсь повторно запустить функцию, она застряла в окне предупреждения, что я делаю неправильно, и кто-нибудь может объяснить, почему это происходит. Вы можете посмотреть комментарий в коде, чтобы увидеть область im, получающую это probemВ чем причина моей функции?

var userChoice = prompt("Do you choose rock, paper or scissors?"); 
var computerChoice = Math.random(); 
    if (computerChoice < 0.34) { 
      computerChoice = "rock"; 
     } else if(computerChoice <= 0.67) { 
      computerChoice = "paper"; 
     } else { 
      computerChoice = "scissors"; 
     } console.log("Computer: " + computerChoice); 
     var compare = function(choice1,choice2){ 
      if (choice1 === choice2){ 
       return "The result is a tie!"; 

      }else if (choice1 === "rock"){ 
      if(choice2 === "scissors"){ 
       return("rock wins"); 
      }else{ 
       return("paper wins"); 
      } 

      }else if (choice1 === "paper"){ 
       if(choice2 === "rock"){ 
        return("paper wins"); 
       }else{ 
        return("scissors wins"); 
       } 
      }else if(choice1 === "scissors"){ 
       if(choice2 === "rock"){ 
        return("rock wins"); 
       }else{ 
        return("scissors wins"); 
       } 
      }else if (choice1 != "rock"&&"paper"&&"scissors"){ 
        alert("not a viable input,please try again"); 
        compare(userChoice,computerChoice); 
    //calling the function here makes the alert box repeatedly pop up 

      } 

     }; 
     compare(userChoice,computerChoice); 
+2

'CHOICE1! = "Рок" && "бумага" && "ножницы"' должен быть 'Choice1! = "Рок" && CHOICE1! = "Бумага" && choice1! = "ножницы". Квази-дубликат http://stackoverflow.com/questions/20002503/why-does-a-b-or-c-or-d-always-evaluate-to-true. – Xufox

+0

Нет, это не проблема, эта часть на самом деле работает нормально, проблема заключается в вызове функции для перезапуска функции. –

+2

У вас есть случай рекурсии, где вход и выход идентичны каждый раз, вызывая бесконечный цикл. Чего вы пытаетесь достичь? – Griffith

ответ

0

Вы можете установить флаг и поместить все в цикл while.

var finished = false; 

var compare = function (choice1, choice2) { 
    if (choice1 === choice2) { 
     finished = true; 
     return "The result is a tie!"; 
    } else if (choice1 === "rock") { 
     if (choice2 === "scissors") { 
     finished = true; 
     return ("rock wins"); 
    } else { 
     finished = true; 
     return ("paper wins"); 
    } 

} else if (choice1 === "paper") { 
    if (choice2 === "rock") { 
     finished = true; 
     return ("paper wins"); 
    } else { 
     finished = true; 
     return ("scissors wins"); 
    } 
} else if (choice1 === "scissors") { 
    if (choice2 === "rock") { 
     finished = true; 
     return ("rock wins"); 
    } else { 
     finished = true; 
     return ("scissors wins"); 
    } 
} else if (choice1 != "rock" && "paper" && "scissors") { 
    alert("not a viable input,please try again"); 
    finished = false; 
    //compare(userChoice, computerChoice); This causes recursion. Not necessary. 

} 

}; 

while (!finished) { 
    var userChoice = prompt("Do you choose rock, paper or scissors?"); 
    var computerChoice = Math.random(); 

if (computerChoice < 0.34) { 
    computerChoice = "rock"; 
} else if (computerChoice <= 0.67) { 
    computerChoice = "paper"; 
} else { 
    computerChoice = "scissors"; 
} 

console.log("Computer: " + computerChoice); 

alert(compare(userChoice, computerChoice)); 
} 

Взгляните на эту скрипку http://jsfiddle.net/7p94axhp/

Смежные вопросы