1

Im trying to make a simple math practice program in javascript. Different values are given in a prompt alert and the answer is compared 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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, 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: jsfiddle.net/695BK/1
1

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.net/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 compare 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.net/695BK/3/

Comments

0

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.net/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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.