-3

I am pretty new to programming and I am trying to make a math game where The javaScript program should allow users to enter two numbers and generate a random mathematical operations (+, -, /, *) I know I have to use math.random to do this but I am not sure where to put it in the code or how to use it properly yet. Can anybody give me a quick run down on how I would do this ?

Sorry for the bad explanation I'll try to be more detailed in what I'm trying to do. This is my code so far

var a = prompt("Enter a number");
var b = prompt("Enter a number");
var correctAnswer = Number(a) + Number(b);
var user=AnswerNumber(prompt("What is " + a + " + " + b));
if(correctAnswer == userAnswer) {
  alert("you are correct");
} else {
  alert("you are NOT correct. The answer is " + correctAnswer );
}

Now I want to be able to make it so uses can enter 2 number and the program generates a random mathematical operation such as (+,-,*,/,) I know math.random can help do this but I am having trouble understanding how and where to put it in the code.

jwvh
  • 50,871
  • 7
  • 38
  • 64

3 Answers3

0

Firstly you will need to store an array of your possible operators i.e.

var operators = ['+', '-', '*', '/']

then you will want to use Math.random to select a random index into this array. Here is a good write-up of how to achieve random integers in a range (0 and the lenth of the array in this case).

once you have chosen a random operator you can use a switch statement to do something appropriate for that operator:

var operator = getRandomOperator()
switch(operator){
    case: '+'
         //do some adding
         break;
    case: '-'
         //do some subtracting
         break;
    .
    .
    .
}

This should give you a starting point!

Community
  • 1
  • 1
dpix
  • 2,765
  • 2
  • 16
  • 25
0

You will (probably) want to use HTML and JS for this. A really good resource for this is W3schools. Here is an example on how to make your game:

<!DOCTYPE html>
<html>
<body>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var p = document.getElementById("demo")
    var x = Math.floor((Math.random() * 100) + 1);
    var numA = prompt("please enter your first number.");
    var numB = prompt("please enter your next number.");
    if (x < 25){
        var out = parseInt(numA) + parseInt(numB);
        p.innerHTML = "+ \n " + x;
    }else if (x > 25 && x < 50){
        var out = parseInt(numA) / parseInt(numB);
        p.innerHTML = "/ \n " + x;
    }else if (x > 50 && x < 75){
        var out = parseInt(numA) - parseInt(numB);
        p.innerHTML = "- \n " + x;
    }else{
        var out = parseInt(numA) * parseInt(numB);
        p.innerHTML = "* \n " + x;
    }
    alert("What did I do? \n input: " + numA + "<operation>" + numB + "\n output: " + out);
}
</script>

</body>
</html>
zoecarver
  • 5,523
  • 2
  • 26
  • 56
0
enter code herevar a = prompt("Enter a number"); var b = prompt("Enter a number"); var randomNum = Math.floor((Math.random () * 4)+ 1); if (randomNum == 1){ var correctAnswer = Number(a) + Number(b); var userAnswer Number(prompt("What is " + a + " + " + b));} else if(randomNum == 2){ var correctAnswer = Number(a) - Number(b); var userAnswer = Number(prompt("What is " + a + " - " + b));} if (randomNum == 3) {var correctAnswer = Number(a) * Number(b); var userAnswer = Number(prompt("What is " + a + " * " + b));}else if(randomNum == 4){ var correctAnswer = Number(a) / Number(b); var userAnswer = Number(prompt("What is " + a + " / " + b));} if(correctAnswer == userAnswer) { alert("you are correct"); } else { alert("you are NOT correct. The answer is " + correctAnswer ); }

thanks everybody for all your help, I'm sorry I didnt know there were so many rules to posting so next time i will make sure my question is not so vauge. the main thing i was having problems with is learning how to use the math.random code, but i have figured it out, this was my final code and everything worked out good.