可以使用不带引号的数组值吗?

时间:2021-02-25 08:19:35

You'll have to forgive me, I'm new to JavaScript...like a few weeks new. Anyway, I created a code using JavaScript to generate two random numbers, ask to add them, and give a "That is correct/that is incorrect" answer based on the users response. I wanted to add the other signs (-,*,/) to the equation and decided to try my hand at arrays to do so. Here is what I have so far:

你必须原谅我,我是JavaScript的新手......就像几个星期新的一样。无论如何,我使用JavaScript创建了一个代码来生成两个随机数,要求添加它们,并根据用户响应给出“那是正确的/那是不正确的”答案。我想将其他符号( - ,*,/)添加到等式中,并决定尝试在数组中执行此操作。这是我到目前为止:

<head>
    <meta charset="utf-8" />
    <title>Math Games</title>
</head>

<body>
    <script>
        var Answer;
        var numbers=new Array();
        var signs=new Array();
        var Signs2=new Array();
        var SignNoQuote=new Array();
        numbers[0]=(Math.floor(Math.random() * 10 + 1));
        numbers[1]=(Math.floor(Math.random() * 10 + 1));
        signs[0]="+";
        signs[1]="-";
        signs[2]="*";
        signs[3]="/";
        SignNoQuote[0]="+";
        SignNoQuote[1]="-";
        SignNoQuote[2]="*";
        SignNoQuote[3]="/";
        Signs2[0]=(Math.floor(Math.random() * 4));          
        Answer=window.prompt("What is " + numbers[0] + signs[Signs2[0]] + numbers[1] + "?");
        if(Answer==numbers[0] + SignNoQuote[Signs2[0]] + numbers[1])
        {
            window.alert("That's Correct!");
        }
        else
        {
            window.alert("That is Incorrect");
        }
    </script>

    <a href="file:///E:/ECS/Legitimate%20Work/mathtest.html">Refresh</a>

</body>

It asks the question correctly, but when the right answer is given, it says that it is incorrect. I tried removing the quotation marks from the values of the "SignNoQuote" array hoping it would work, but when it is run that way, none of the script will run and the debugger claims it to be a syntax error? What am I doing wrong and how can I fix it?

它正确地问了这个问题,但是当给出正确的答案时,它说它是不正确的。我尝试从“SignNoQuote”数组的值中删除引号,希望它可以工作,但是当它以这种方式运行时,脚本都不会运行,调试器声称它是语法错误?我做错了什么,我该如何解决?

3 个解决方案

#1


5  

If you want something specific to the use case you have, this will work nicely:

如果你想要一个特定于你的用例的东西,这将很好地工作:

//A mapping from the symbol for an operation to
//the function that executes it.
var opFunction = {
    "+": function (x, y) { return x + y; },
    "-": function (x, y) { return x - y; },
    "*": function (x, y) { return x * y; },
    "/": function (x, y) { return x / y; }
};

//Gets the operation symbol.
var op = SignNoQuote[Signs2[0]];

//Looks up the function for the operation,
//then calls it with your numbers as operands.
var result = opFunction[op](numbers[0], numbers[1]);

However, if you need something general purpose for evaluating mathematical expressions, Brad's answer provides what you need.

但是,如果您需要一些评估数学表达式的通用目的,Brad的答案将提供您所需要的。

#2


3  

If I'm understanding the problem, you are trying to use a string of a symbol to do math.

如果我正在理解这个问题,那么你正在尝试使用符号字符串来进行数学运算。

numbers[0] + SignNoQuote[Signs2[0]] + numbers[1]

The trouble with this is that + means concatenation when used in the context of strings. You don't have a + operator in code, you have the string of text with one character, +. These are fundamentally different. It's no different than this:

这样做的问题在于+表示在字符串的上下文中使用时的连接。代码中没有+运算符,你有一个带有一个字符的文本字符串+。这些根本不同。它没有什么不同:

numbers[0] + '+' + numbers[1]

.... which results in something like this:

....导致这样的事情:

"1+2"

What you need to do is actually execute that input somehow, such as with eval(). Unfortunately, this is where you run into some scary security issues, allowing users to execute whatever they want. In some contexts this can be safe, but usually it isn't.

你需要做的是以某种方式执行该输入,例如使用eval()。不幸的是,这是您遇到一些可怕的安全问题,允许用户执行他们想要的任何事情。在某些情况下,这可能是安全的,但通常不是。

There are options for you to run this equation. See this question for details: Evaluating a string as a mathematical expression in JavaScript

您可以选择运行此等式。有关详细信息,请参阅此问题:在JavaScript中将字符串作为数学表达式进行评估

#3


0  

When you're checking you're answer, you use a comparison to:

当你检查你的答案时,你使用比较:

numbers[0] + SignNoQuote[Signs2[0]] + numbers[1]

But assuming you want it to multiply, what that actually is, is something like:

但假设你希望它成倍增加,那实际上就是:

2 + "*" + 1

Which is not the same as:

哪个不一样:

2 * 1

The easiest way to do this would probably be with a if ... else if ... else if ... statement. Something like:

最简单的方法可能是使用if ... else if ... else if ...语句。就像是:

var answerCorrect = false;
if (SignNoQuote[Signs2[0]] == "+") {
    answerCorrect = (Answer == numbers[0] + numbers[1]);
} else if (SignNoQuote[Signs2[0]] == "*") {
    answerCorrect = (Answer == numbers[0] * numbers[1]);
} else if (etc ...

then:

if(answerCorrect) {
    window.alert("That's Correct!");
} else {
    window.alert("That is Incorrect");
}

#1


5  

If you want something specific to the use case you have, this will work nicely:

如果你想要一个特定于你的用例的东西,这将很好地工作:

//A mapping from the symbol for an operation to
//the function that executes it.
var opFunction = {
    "+": function (x, y) { return x + y; },
    "-": function (x, y) { return x - y; },
    "*": function (x, y) { return x * y; },
    "/": function (x, y) { return x / y; }
};

//Gets the operation symbol.
var op = SignNoQuote[Signs2[0]];

//Looks up the function for the operation,
//then calls it with your numbers as operands.
var result = opFunction[op](numbers[0], numbers[1]);

However, if you need something general purpose for evaluating mathematical expressions, Brad's answer provides what you need.

但是,如果您需要一些评估数学表达式的通用目的,Brad的答案将提供您所需要的。

#2


3  

If I'm understanding the problem, you are trying to use a string of a symbol to do math.

如果我正在理解这个问题,那么你正在尝试使用符号字符串来进行数学运算。

numbers[0] + SignNoQuote[Signs2[0]] + numbers[1]

The trouble with this is that + means concatenation when used in the context of strings. You don't have a + operator in code, you have the string of text with one character, +. These are fundamentally different. It's no different than this:

这样做的问题在于+表示在字符串的上下文中使用时的连接。代码中没有+运算符,你有一个带有一个字符的文本字符串+。这些根本不同。它没有什么不同:

numbers[0] + '+' + numbers[1]

.... which results in something like this:

....导致这样的事情:

"1+2"

What you need to do is actually execute that input somehow, such as with eval(). Unfortunately, this is where you run into some scary security issues, allowing users to execute whatever they want. In some contexts this can be safe, but usually it isn't.

你需要做的是以某种方式执行该输入,例如使用eval()。不幸的是,这是您遇到一些可怕的安全问题,允许用户执行他们想要的任何事情。在某些情况下,这可能是安全的,但通常不是。

There are options for you to run this equation. See this question for details: Evaluating a string as a mathematical expression in JavaScript

您可以选择运行此等式。有关详细信息,请参阅此问题:在JavaScript中将字符串作为数学表达式进行评估

#3


0  

When you're checking you're answer, you use a comparison to:

当你检查你的答案时,你使用比较:

numbers[0] + SignNoQuote[Signs2[0]] + numbers[1]

But assuming you want it to multiply, what that actually is, is something like:

但假设你希望它成倍增加,那实际上就是:

2 + "*" + 1

Which is not the same as:

哪个不一样:

2 * 1

The easiest way to do this would probably be with a if ... else if ... else if ... statement. Something like:

最简单的方法可能是使用if ... else if ... else if ...语句。就像是:

var answerCorrect = false;
if (SignNoQuote[Signs2[0]] == "+") {
    answerCorrect = (Answer == numbers[0] + numbers[1]);
} else if (SignNoQuote[Signs2[0]] == "*") {
    answerCorrect = (Answer == numbers[0] * numbers[1]);
} else if (etc ...

then:

if(answerCorrect) {
    window.alert("That's Correct!");
} else {
    window.alert("That is Incorrect");
}