How to get random math operator for a quiz qustion in javascript

Tall boY

I want to know how can we get a random math operator to use and process in a math quiz question.

A random question is given to a user to solve, question has only two number to solve for any of the addition, subtraction, multiplication or division.

I have this code to generate two random number.

HTML

    <div id="num1"></div><!--end of num1-->
    <div id="num2"></div><!--end of num2-->
    <div id="operator"></div><!--end of operator-->  
    <div id="answer"></div><!--end of answer-->
    <button onclick="New()">New</button>

Javascript

function New(){
num1=document.getElementById("num1");
num2=document.getElementById("num2");   
rnum1 = Math.floor((Math.random()*100)+1);
rnum2 = Math.floor((Math.random()*100)+1);
num1.innerHTML=rnum1
num2.innerHTML=rnum2    
}

How can I generate random operator from +-*/ to use and process in code like

operator.innerHTML = '+';
answer.innerHTML = rnum1 + rnum2;
Derek 朕會功夫

You can do this:

var operators = [{
        sign: "+",
        method: function(a,b){ return a + b; }
    },{
        sign: "-",
        method: function(a,b){ return a - b; }
    }];

var selectedOperator = Math.floor(Math.random()*operators.length);

operators[selectedOperator].sign                  //this will give you the sign
operators[selectedOperator].method(rnum1, rnum2)  //this will give you the answer

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get random math operator for a quiz qustion in javascript

From Dev

How to make a random number math quiz with 10 questions in C++?

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to get unique numbers from Math.random in javascript?

From Dev

How to use math.random again to get a different value if the values are the same in javascript

From Dev

Javascript Math.random() not *that* random…?

From Dev

Math & JavaScript | Random chances

From Dev

how to repeat result from math.random in javascript?

From Dev

How to get some math errors in JavaScript

From Dev

Javascript - Math.random as parameter

From Java

How to have images in a JavaScript quiz?

From Dev

how to use loops in a javascript quiz

From Dev

Java Math.random() How random is it?

From Dev

Get rid of check answer button in this javascript quiz?

From Dev

How to get key of random chosen object in JavaScript?

From Dev

How to get a random character from a range in JavaScript?

From Dev

How do you get random RGB in Javascript?

From Dev

Javascript quiz - display choices in a random order using Array.prototype

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

What does the operator/math sign ">>" mean in Javascript?

From Dev

javascript TypeError: Math.random is not a function on chrome

From Dev

Math.random and web programming in JavaScript

From Dev

Math.random() javascript function *undefined* on Chrome

From Dev

Javascript Math.random() and conditional expressions

From Dev

How do I generate random quiz questions and score them?

From Dev

How to get Math.random() to add to returned values rather than replacing them

From Dev

How do I get the full decimal expansion from Perl's Math::Random::Secure?

From Dev

how to set answer to image in javascript quiz game

From Dev

Generate a random math Equation using Random numbers and operators in Javascript

Related Related

  1. 1

    How to get random math operator for a quiz qustion in javascript

  2. 2

    How to make a random number math quiz with 10 questions in C++?

  3. 3

    How to get unique numbers from Math.random in javascript?

  4. 4

    How to get unique numbers from Math.random in javascript?

  5. 5

    How to use math.random again to get a different value if the values are the same in javascript

  6. 6

    Javascript Math.random() not *that* random…?

  7. 7

    Math & JavaScript | Random chances

  8. 8

    how to repeat result from math.random in javascript?

  9. 9

    How to get some math errors in JavaScript

  10. 10

    Javascript - Math.random as parameter

  11. 11

    How to have images in a JavaScript quiz?

  12. 12

    how to use loops in a javascript quiz

  13. 13

    Java Math.random() How random is it?

  14. 14

    Get rid of check answer button in this javascript quiz?

  15. 15

    How to get key of random chosen object in JavaScript?

  16. 16

    How to get a random character from a range in JavaScript?

  17. 17

    How do you get random RGB in Javascript?

  18. 18

    Javascript quiz - display choices in a random order using Array.prototype

  19. 19

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  20. 20

    What does the operator/math sign ">>" mean in Javascript?

  21. 21

    javascript TypeError: Math.random is not a function on chrome

  22. 22

    Math.random and web programming in JavaScript

  23. 23

    Math.random() javascript function *undefined* on Chrome

  24. 24

    Javascript Math.random() and conditional expressions

  25. 25

    How do I generate random quiz questions and score them?

  26. 26

    How to get Math.random() to add to returned values rather than replacing them

  27. 27

    How do I get the full decimal expansion from Perl's Math::Random::Secure?

  28. 28

    how to set answer to image in javascript quiz game

  29. 29

    Generate a random math Equation using Random numbers and operators in Javascript

HotTag

Archive