javascript函数if else。 Pico Fermi Bagel游戏

时间:2016-11-19 02:14:28

标签: javascript function parsing if-statement

而不是“你赢了”我得到了“Pico Pico Pico ?? 为什么回复pico pico pico而不是你赢...因为最后的声明是你赢了.......... 这是游戏规则....... 计算机生成一个三位数字,其中每个数字必须是唯一的。提供无限次转动来猜测数字。计算机可以提供以下提示以响应每个猜测:

           Pico: One digit is correct but in the wrong place
            Fermi: One digit is correct and in the right place
            Bagels: No digits are correct

// global V
var compChoice1, compChoice2, compChoice3;
var number1, number2, number3;
var response;
//functions to create random numbers
function getRandomNum() {
    return Math.floor(Math.random() * 10)
}

function startGame(){
    do{
        compChoice1 = getRandomNum();
        compChoice2 = getRandomNum();
        compChoice3 = getRandomNum();
    }
    while (compChoice1 == compChoice2 || compChoice1 == compChoice3 || compChoice2 == compChoice3);
}


//here we'll get the usr Numnber
function userNum(){
  number1 = document.getElementById('guessNum1').value;
  number2 = document.getElementById('guessNum2').value;
  number3 = document.getElementById('guessNum3').value;
    console.log(number1,number2,number3);
}

    // Compare Usr numbers vs computer ramdom nums
    function compareUsrvsCom(){

        var response = "";


        if (number1 == compChoice1) response += "pico  ";
        else if (number1 == compChoice2 || number1 == compChoice3) response += "fermi  ";


        if (number2 == compChoice2) response += "pico  ";
        else if (number2 == compChoice1 || number2 == compChoice3) response += "fermi  ";


        if (number3 == compChoice3) response += "pico  ";
        else if (number3 == compChoice1 || number3 == compChoice2) response += "fermi  ";


        if (number1 === compChoice1 && number2 === compChoice2 && number3 === compChoice3) response += "You win";
        else if (response == "" ) return ("beagls   ")


        return response;
        // return response;
    }
       //Start game Btn
document.getElementById('startGame').addEventListener('click', function() {
       startGame();
       console.log(compChoice1,compChoice2, compChoice3);
   });

   document.getElementById("guessButn").addEventListener("click", function() {
     userNum();
     compareUsrvsCom();
     console.log(compareUsrvsCom());
       document.getElementById("demoBagel").innerHTML = compareUsrvsCom();
     });

1 个答案:

答案 0 :(得分:1)

=====

不同

问题是您有两种类型要比较。与

number1 = document.getElementById('guessNum1').value;

您可以通过

获得string
console.log(typeof number1);

使用compChoice1,您获得了number。你可以用

看到
console.log(typeof Math.floor(Math.random() * 10));

您正在混合两个比较运算符=======运算符不太严格。

5 == "5"为true,但5 === "5"为false。

这就是为什么你赢了"如果陈述失败。您应该几乎总是使用===运算符,并确保您正在比较相同的类型。

因此我们要将guessNum1的值解析为整数,您可以使用parseInt进行解析。

使用parseInt将用户输入转换为number类型

function userNum(){
   number1 = parseInt(document.getElementById('guessNum1').value);
   number2 = parseInt(document.getElementById('guessNum2').value);
   number3 = parseInt(document.getElementById('guessNum3').value);
   console.log(number1,number2,number3, typeof number1);
}

这将为您提供所有number类型。

首选===

所以转换==

中的比较运算符
compareUsrvsCom