嵌套的if-else语句被跳过 [英] Nested If-else statements being skipped

查看:328
本文介绍了嵌套的if-else语句被跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在这里计算机产生一个随机数(1-100),用户必须猜出正确的数字游戏。我们的目标是为计算机当前的猜测比较到previous猜测,吐出一份声明:热,冷,热,冷的,等等。

What I'm building is a game where the computer generates a random number (1-100) and the user must guess the correct number. The goal is for the computer to compare the current guess to the previous guess and spit out a statement: "hot", "cold", "hotter", "colder", etc.

我的code(专注于JS): codePEN小提琴

My Code (focus on the JS): CodePen fiddle

//global variables--computer generated guess, and guess log 
var answer = Math.floor((Math.random() * 100)+1); 
var guessArray = []; 
var index = 0; 

//user clicks submit button and guess is registered by computer
$("#submit").click( function(){
    var guess = $("#guess").val(); 

    guessArray.push(guess);
    //prints out the answer and user guesses
    $("#answer").text("Answer:" + " "+ answer);
    $("#guessArrayPrint").text("You guessed: " + " " + guessArray + " ");

    if (answer === guess) {
        $("#statement").text("woo hoo right answer");
    } else {
        var currentDifference = Math.abs(answer-guess);
        var currentDiffArray = [];

        currentDiffArray.push(currentDifference); 

        if (index = 0)  {   
            //if-else statement comparing current guess range to answer
            if ( currentDifference >=1 && currentDifference <= 10){
                $("#statement").text("Ouch! You're hot!");
            } else {
                $("#statement").text("Brr! You're cold!");
            }
        } else { 
            //if-else statement comparing current guess to previous guess
            var previousDiff = answer- prevguess;
            var prevguess = guessArray [i-1];

            if( previousDiff < currentDifference){
                $("#statement").text("Ahh! Getting Warmer!");
            } else {
                $("#statement").text("Brrr...getting colder");
            }
        }
        index++
    }
});

我的嵌套的if-else语句不工作。当用户输入一个猜测,无论多么接近的答案,它总是返回声明的BRR ..变冷的,这是在其他一节。

在理想情况下,当用户输入他们的第一个猜想如果(指数= 0)应该运行,那么当第二个猜测是输入,就应该移动到其他语句在previous猜测变量。我想围绕着变数移动,改变的if / else订单,并想也许这就是 ++指数的位置。没有什么工作。不知道什么是错的我的变量,数组或我的if / else语句的语法。

Ideally when the user inputs their first guess if (index = 0) should run then when the second guess is input, it should move to the "else" statement with the previous guess variables. I tried moving around the variables, changed orders of if/else, and thought maybe it's the placement of index++. Nothing is working. Not sure if something is wrong with my variables , arrays, or the syntax of my if/else statements.

TL;博士:当程序只运行了其他部分中的嵌套的if-else 语句运行。不知道如何解决......我已经通过我的code多次了。语法,数组和变量。不确定什么是错的。

tl;dr: when the program is run only the "else" portion of the nested if-else statement is run. Not sure how to fix… I've gone through my code a number of times. The syntax, the arrays, and variables. Uncertain what's wrong.

推荐答案

您有JS 如果(指数= 0)。这应该是如果(指数=== 0)

You JS has if (index = 0). This should be if (index === 0).

此外,你需要在你输入字段的值转换为数值。为此,可以使用这样做:

Additionally, you need to cast the value of your input field to a number. You can do this using:

var guess = +$("#guess").val(); // + cast as a number

更多语法错误:

prevguess = guessArray[i - 1] --> prevguess = guessArray[index - 1];

下面是部分工作小提琴。我通过一些场景跑去,小提琴真的只是如果你给应用程序的正确答案的作品。在code有许多语法错误,糟糕的裁判和计算。我建议打开控制台或调试器,识别这些问题,并修复它们。

Here is a partial working Fiddle. I ran through some scenarios, and the fiddle really only works if you give the application the right answer. The code has many syntax errors, bad refs and calculations. I would suggest opening the console or a debugger, identifying those issue, and fixing them.

下面是一个全功能演示

这篇关于嵌套的if-else语句被跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆