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;
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;
Share
Improve this question
asked Jan 20, 2014 at 5:48
user1411607user1411607
5 Answers
Reset to default 7You 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
I know I'm a little late to the party, but I thought I'd share a nice little hash map I wrote that takes advantage of the ES6 arrow functions for clarity.
const OPMAP = {
'*': (n1, n2) => n1 * n2,
'/': (n1, n2) => n1 / n2,
'+': (n1, n2) => n1 + n2,
'-': (n1, n2) => n1 - n2
}
Simply call OPMAP[operator] to get your anonymous function, and OPMAP[operator](n1, n2) to get your result.
Define an array of operators:
var ops=['+','-','*','/'];
//random operator
var opindex = Math.random()*4; //good that your rnum2 cannot be zero
var operator = ops[opindex];
//calculate the expected result:
var res;
switch (opindex){
case 0: res=rnum1+rnum2; break;
case 1: res=rnum1-rnum2; break;
case 2: res=rnum1*rnum2; break;
case 3: res=rnum1/rnum2; break;
}
I would like to share my approach for generating random basic operations. I want it to have a bit of control in regards to the operations/operators being selected thus, selection of these elements is done by range, e.g. Math.random() returns a number between 0..1, thus if the value is lower than 0.5 it uses a given operator, in other words, the given operator has 50% of changes of being selected. Here is the full implementation, (for now it also supports adding one variable X in the equation):
function _getRandomNumber() {
var randomPercentage = Math.random();
var cardinality = (randomPercentage <= 0.20) ? -1.0 : 1.0;
randomPercentage = Math.random();
var randomNumber = Math.round((randomPercentage * 1000000) * 100) / 100;
return (cardinality * randomNumber);
}
function _getOperator() {
var randomPercentage = Math.random();
if (randomPercentage < 0.10) {
// Nested expression
return `(${_getExpression()})`;
} else if (randomPercentage < 0.20) {
// a header (variable)
return `x`;
} else {
return _getRandomNumber();
}
}
function _getBinaryOperator() {
var randomPercentage = Math.random();
if (randomPercentage < 0.25) {
return `+`;
} else if (randomPercentage < 0.50) {
return `-`;
} else if (randomPercentage < 0.75) {
return `*`;
} else {
return `/`;
}
}
function _getBinaryExpression() {
return `${_getOperator()} ${_getBinaryOperator()} ${_getOperator()}`;
}
function _getExpression() {
return _getBinaryExpression();
}
module.exports = {
"getExpression": _getExpression
};
console.log(_getExpression());
Cheers, Herb
Example:
var operators = ['+','-','*','/'];
function New(){
num1=document.getElementById("num1");
num2=document.getElementById("num2");
oper=document.getElementById("operator");
answer=document.getElementById("answer");
rnum1 = Math.floor((Math.random()*100)+1);
rnum2 = Math.floor((Math.random()*100)+1);
op = operators[Math.floor(Math.random()*4)];
num1.innerHTML=rnum1;
num2.innerHTML=rnum2;
oper.innerHTML=op;
answer.innerHTML = eval(rnum1 + op + rnum2);
}