html - JavaScript prompt doesn't appear - Stack Overflow

Im trying to make a simple math practice program in javascript. Different values are given in a prompt

Im trying to make a simple math practice program in javascript. Different values are given in a prompt alert and the answer is pared with the user input. This is the code:

<html>
<head>
<title>Calculations</title>

<script language = "javascript">

generate();

function generate() {
{   
var calc = Math.floor(math.random() * 3 + 1);


if (calc == 1){
    calcSort == "+"
}else if(calc == 2){
    calcSort == "*";
} else if (calc == 3){
    calcSort == "/";
}

var num1 = Math.floor(Math.random() * 10 + 1);
var num2 = Math.floor(Math.random() * 10 + 1);

var answer;

if (calc == 1){
    answer = num1 + num2;
} else if (calc == 2){
    answer = num1 * num2;
} else if (calc == 3){
    answer = num1 / num2;
}

var userAnswer = prompt("What is " + num1 + calcSort + num2 + "?");

if (userAnswer == answer){
    alert("correct");
} else {
    alert("fail");
}


}

</script>

</head>
</html>

The prompt doesn't appear, what's wrong?

Im trying to make a simple math practice program in javascript. Different values are given in a prompt alert and the answer is pared with the user input. This is the code:

<html>
<head>
<title>Calculations</title>

<script language = "javascript">

generate();

function generate() {
{   
var calc = Math.floor(math.random() * 3 + 1);


if (calc == 1){
    calcSort == "+"
}else if(calc == 2){
    calcSort == "*";
} else if (calc == 3){
    calcSort == "/";
}

var num1 = Math.floor(Math.random() * 10 + 1);
var num2 = Math.floor(Math.random() * 10 + 1);

var answer;

if (calc == 1){
    answer = num1 + num2;
} else if (calc == 2){
    answer = num1 * num2;
} else if (calc == 3){
    answer = num1 / num2;
}

var userAnswer = prompt("What is " + num1 + calcSort + num2 + "?");

if (userAnswer == answer){
    alert("correct");
} else {
    alert("fail");
}


}

</script>

</head>
</html>

The prompt doesn't appear, what's wrong?

Share Improve this question asked Nov 19, 2013 at 21:21 Wilhelm MichaelsenWilhelm Michaelsen 66314 silver badges33 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You have an extra { at the beginning of the function declaration for generate(), also math is not defined because you need to use Math (case-sensitive) to use the utility functions.

Comment on Alex W's answer moved here:

In addition to Alex W's answer, you're trying to assign calcSort using the equality operator (==) instead of the assignment operator (=), and you are defining calcSort in global scope (bad). Here's a working jsFiddle: http://jsfiddle/695BK/1

Also, just for fun, I thought I would show you how you could easily clean up your code a bit to make it more maintainable. The multiple code blocks that pare calc to a number is, to me, a code smell. I tucked away all the logic for each calc result into a array of little helper objects. Then you can randomly pick an operation from the array. When you want to add another operation, you simply add an item to the operations array:

function generate() {

    var operations = [
        { symbol: '+', calc: function(a, b) { return a + b; }}, // 0
        { symbol: '*', calc: function(a, b) { return a * b; }}, // 1
        { symbol: '/', calc: function(a, b) { return a / b; }}  // 2
    ];

    var calc = Math.floor(Math.random() * operations.length + 1) - 1;
    var operation = operations[calc];

    var num1 = Math.floor(Math.random() * 10 + 1);
    var num2 = Math.floor(Math.random() * 10 + 1);

    var answer = operation.calc(num1, num2);

    var userAnswer = prompt("What is " + num1 + operation.symbol + num2 + "?");

    if (userAnswer == answer) {
        alert("correct");
    } else {
        alert("fail");
    }
}

Here's a working demo of the above: http://jsfiddle/695BK/3/

Several problems:

  • You have an extra { at the start of your function
  • JS is case sensitive, so use Math not math
  • You used == instead of = to try to assign values to calcSort

Here's a working version: http://jsfiddle/Tfb97/

Note that you should declare all of your variables with var or they'll be global - currently you don't declare calcSort.

Note also that the first two problems above could be found if you check your browser's JS console for errors. The missing { would give an "Unexpected end of input" error, while the second would give "ReferenceError: math is not defined". (The == versus = thing is not actually a syntax error so it won't be reported in the console.)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745138593a4613322.html

相关推荐

  • html - JavaScript prompt doesn&#39;t appear - Stack Overflow

    Im trying to make a simple math practice program in javascript. Different values are given in a prompt

    10小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信