JavaScript to generate the questions using random numbers using Select operators randomly(+,*,-) for each...












0















I want to do this math game that generates numbers and operators randomly.



This is hint - x = Math.floor(Math.random()*101); will generate a random number between 0 and 100. Select operators randomly (+,*,-) for each equation. Please any one can help me.






var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);












share|improve this question

























  • StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 27 '18 at 3:13











  • var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

    – siaylum
    Nov 27 '18 at 3:18











  • ok, edited. @Andreas.

    – siaylum
    Nov 27 '18 at 3:22
















0















I want to do this math game that generates numbers and operators randomly.



This is hint - x = Math.floor(Math.random()*101); will generate a random number between 0 and 100. Select operators randomly (+,*,-) for each equation. Please any one can help me.






var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);












share|improve this question

























  • StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 27 '18 at 3:13











  • var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

    – siaylum
    Nov 27 '18 at 3:18











  • ok, edited. @Andreas.

    – siaylum
    Nov 27 '18 at 3:22














0












0








0








I want to do this math game that generates numbers and operators randomly.



This is hint - x = Math.floor(Math.random()*101); will generate a random number between 0 and 100. Select operators randomly (+,*,-) for each equation. Please any one can help me.






var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);












share|improve this question
















I want to do this math game that generates numbers and operators randomly.



This is hint - x = Math.floor(Math.random()*101); will generate a random number between 0 and 100. Select operators randomly (+,*,-) for each equation. Please any one can help me.






var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);








var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);





var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
document.write(arr);






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 3:29









Yong Quan

2,7382927




2,7382927










asked Nov 27 '18 at 3:12









siaylumsiaylum

84




84













  • StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 27 '18 at 3:13











  • var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

    – siaylum
    Nov 27 '18 at 3:18











  • ok, edited. @Andreas.

    – siaylum
    Nov 27 '18 at 3:22



















  • StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

    – Andreas
    Nov 27 '18 at 3:13











  • var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

    – siaylum
    Nov 27 '18 at 3:18











  • ok, edited. @Andreas.

    – siaylum
    Nov 27 '18 at 3:22

















StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

– Andreas
Nov 27 '18 at 3:13





StackOverflow is not a code-writing service. Please read through the Help Center, in particular How do I ask a good question? If you run into a specific problem, research it thoroughly, search thoroughly here, and if you're still stuck post your code and a description of the problem. Also, remember to include Minimum, Complete, Verifiable Example. People will be glad to help

– Andreas
Nov 27 '18 at 3:13













var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

– siaylum
Nov 27 '18 at 3:18





var arr = while(arr.length < 8){ var r = Math.floor(Math.random()*100) + 1; if(arr.indexOf(r) === -1) arr.push(r); } document.write(arr); I wrote like this code for take random numbers. this is working properly. Now, I need to add math operations for that. How I can do that.@Andreas

– siaylum
Nov 27 '18 at 3:18













ok, edited. @Andreas.

– siaylum
Nov 27 '18 at 3:22





ok, edited. @Andreas.

– siaylum
Nov 27 '18 at 3:22












3 Answers
3






active

oldest

votes


















4














Here's an ES6 implementation of that, I used console.log instead of document.write






let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});








share|improve this answer
























  • Thank you for your answer my friend, how to add 2 marks per correct answer).

    – siaylum
    Nov 27 '18 at 4:09



















0














You can use switch-case statement to select the operator and use Math.random() to randomize it



var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}

for (var i=0; i<4; i++) {
operator = Math.floor(Math.random() * 3);
switch(operator) {
case 0:
eval(prompt(arr[i*2] + " + " + arr[i*2+1]));
break;
case 1:
eval(prompt(arr[i*2] + " - " + arr[i*2+1]));
break;
case 2:
eval(prompt(arr[i*2] + " * " + arr[i*2+1]));
break;
}
}





share|improve this answer


























  • How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

    – siaylum
    Nov 27 '18 at 3:57











  • Edited my answer to use eval(prompt())

    – Andreas
    Nov 27 '18 at 4:01











  • Thank you for your answer my friend, how to add 2 marks per correct answer)

    – siaylum
    Nov 27 '18 at 4:12











  • Please attempt that by yourself first

    – Andreas
    Nov 27 '18 at 4:13



















0














Better solution: While it doesn't use prompts I'm sure you can figure that part out.

works best on codepen, and not very well inside the awkward snippet

feature that stack-overflow provides (due to the reset button triggering reload)

here's the code in case your interested:






//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});








share|improve this answer
























  • Thank you for the your answer my friend.

    – siaylum
    Nov 28 '18 at 2:27











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492182%2fjavascript-to-generate-the-questions-using-random-numbers-using-select-operators%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














Here's an ES6 implementation of that, I used console.log instead of document.write






let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});








share|improve this answer
























  • Thank you for your answer my friend, how to add 2 marks per correct answer).

    – siaylum
    Nov 27 '18 at 4:09
















4














Here's an ES6 implementation of that, I used console.log instead of document.write






let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});








share|improve this answer
























  • Thank you for your answer my friend, how to add 2 marks per correct answer).

    – siaylum
    Nov 27 '18 at 4:09














4












4








4







Here's an ES6 implementation of that, I used console.log instead of document.write






let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});








share|improve this answer













Here's an ES6 implementation of that, I used console.log instead of document.write






let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});








let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});





let questionsCount = 10;

function generateQuestions() {
let questions = ;

for(let i = 0;i < questionsCount;i++) {
let n1 = Math.floor(Math.random() * 101);
let n2 = Math.floor(Math.random() * 101);
let operatorObj = randomOperator(n1,n2)
questions.push({
question: `${n1} ${operatorObj.operator} ${n2} ?`,
answer: operatorObj.answer
});
}

return questions;
}




function randomOperator(n1,n2) {
let n = Math.floor(Math.random() * 4);
switch (n) {
case 0:
return {operator: '+', answer: n1 + n2};
case 1:
return {operator: '-', answer: n1 - n2}
case 2:
return {operator: '*', answer: n1 * n2}
case 3:
return {operator: '/', answer: n1 / n2}
}
}

let questions = generateQuestions();

let answers = questions.map((question) =>
prompt(question.question));

answers.forEach((answer,i) => {
console.log(`${questions[i].question} ${answer} ${(answer ==
questions[i].answer) ? 'Correct': `Wrong should be
${questions[i].answer}`}`)
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 3:53









onecompilemanonecompileman

4715




4715













  • Thank you for your answer my friend, how to add 2 marks per correct answer).

    – siaylum
    Nov 27 '18 at 4:09



















  • Thank you for your answer my friend, how to add 2 marks per correct answer).

    – siaylum
    Nov 27 '18 at 4:09

















Thank you for your answer my friend, how to add 2 marks per correct answer).

– siaylum
Nov 27 '18 at 4:09





Thank you for your answer my friend, how to add 2 marks per correct answer).

– siaylum
Nov 27 '18 at 4:09













0














You can use switch-case statement to select the operator and use Math.random() to randomize it



var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}

for (var i=0; i<4; i++) {
operator = Math.floor(Math.random() * 3);
switch(operator) {
case 0:
eval(prompt(arr[i*2] + " + " + arr[i*2+1]));
break;
case 1:
eval(prompt(arr[i*2] + " - " + arr[i*2+1]));
break;
case 2:
eval(prompt(arr[i*2] + " * " + arr[i*2+1]));
break;
}
}





share|improve this answer


























  • How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

    – siaylum
    Nov 27 '18 at 3:57











  • Edited my answer to use eval(prompt())

    – Andreas
    Nov 27 '18 at 4:01











  • Thank you for your answer my friend, how to add 2 marks per correct answer)

    – siaylum
    Nov 27 '18 at 4:12











  • Please attempt that by yourself first

    – Andreas
    Nov 27 '18 at 4:13
















0














You can use switch-case statement to select the operator and use Math.random() to randomize it



var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}

for (var i=0; i<4; i++) {
operator = Math.floor(Math.random() * 3);
switch(operator) {
case 0:
eval(prompt(arr[i*2] + " + " + arr[i*2+1]));
break;
case 1:
eval(prompt(arr[i*2] + " - " + arr[i*2+1]));
break;
case 2:
eval(prompt(arr[i*2] + " * " + arr[i*2+1]));
break;
}
}





share|improve this answer


























  • How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

    – siaylum
    Nov 27 '18 at 3:57











  • Edited my answer to use eval(prompt())

    – Andreas
    Nov 27 '18 at 4:01











  • Thank you for your answer my friend, how to add 2 marks per correct answer)

    – siaylum
    Nov 27 '18 at 4:12











  • Please attempt that by yourself first

    – Andreas
    Nov 27 '18 at 4:13














0












0








0







You can use switch-case statement to select the operator and use Math.random() to randomize it



var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}

for (var i=0; i<4; i++) {
operator = Math.floor(Math.random() * 3);
switch(operator) {
case 0:
eval(prompt(arr[i*2] + " + " + arr[i*2+1]));
break;
case 1:
eval(prompt(arr[i*2] + " - " + arr[i*2+1]));
break;
case 2:
eval(prompt(arr[i*2] + " * " + arr[i*2+1]));
break;
}
}





share|improve this answer















You can use switch-case statement to select the operator and use Math.random() to randomize it



var arr = 
while(arr.length < 8){
var r = Math.floor(Math.random()*100) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}

for (var i=0; i<4; i++) {
operator = Math.floor(Math.random() * 3);
switch(operator) {
case 0:
eval(prompt(arr[i*2] + " + " + arr[i*2+1]));
break;
case 1:
eval(prompt(arr[i*2] + " - " + arr[i*2+1]));
break;
case 2:
eval(prompt(arr[i*2] + " * " + arr[i*2+1]));
break;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 27 '18 at 4:00

























answered Nov 27 '18 at 3:29









AndreasAndreas

1,97931018




1,97931018













  • How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

    – siaylum
    Nov 27 '18 at 3:57











  • Edited my answer to use eval(prompt())

    – Andreas
    Nov 27 '18 at 4:01











  • Thank you for your answer my friend, how to add 2 marks per correct answer)

    – siaylum
    Nov 27 '18 at 4:12











  • Please attempt that by yourself first

    – Andreas
    Nov 27 '18 at 4:13



















  • How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

    – siaylum
    Nov 27 '18 at 3:57











  • Edited my answer to use eval(prompt())

    – Andreas
    Nov 27 '18 at 4:01











  • Thank you for your answer my friend, how to add 2 marks per correct answer)

    – siaylum
    Nov 27 '18 at 4:12











  • Please attempt that by yourself first

    – Andreas
    Nov 27 '18 at 4:13

















How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

– siaylum
Nov 27 '18 at 3:57





How to show this equation in prompt. I wrote like this, but this is not working properly. eval(prompt (" + "));

– siaylum
Nov 27 '18 at 3:57













Edited my answer to use eval(prompt())

– Andreas
Nov 27 '18 at 4:01





Edited my answer to use eval(prompt())

– Andreas
Nov 27 '18 at 4:01













Thank you for your answer my friend, how to add 2 marks per correct answer)

– siaylum
Nov 27 '18 at 4:12





Thank you for your answer my friend, how to add 2 marks per correct answer)

– siaylum
Nov 27 '18 at 4:12













Please attempt that by yourself first

– Andreas
Nov 27 '18 at 4:13





Please attempt that by yourself first

– Andreas
Nov 27 '18 at 4:13











0














Better solution: While it doesn't use prompts I'm sure you can figure that part out.

works best on codepen, and not very well inside the awkward snippet

feature that stack-overflow provides (due to the reset button triggering reload)

here's the code in case your interested:






//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});








share|improve this answer
























  • Thank you for the your answer my friend.

    – siaylum
    Nov 28 '18 at 2:27
















0














Better solution: While it doesn't use prompts I'm sure you can figure that part out.

works best on codepen, and not very well inside the awkward snippet

feature that stack-overflow provides (due to the reset button triggering reload)

here's the code in case your interested:






//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});








share|improve this answer
























  • Thank you for the your answer my friend.

    – siaylum
    Nov 28 '18 at 2:27














0












0








0







Better solution: While it doesn't use prompts I'm sure you can figure that part out.

works best on codepen, and not very well inside the awkward snippet

feature that stack-overflow provides (due to the reset button triggering reload)

here's the code in case your interested:






//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});








share|improve this answer













Better solution: While it doesn't use prompts I'm sure you can figure that part out.

works best on codepen, and not very well inside the awkward snippet

feature that stack-overflow provides (due to the reset button triggering reload)

here's the code in case your interested:






//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});








//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});





//Solve the equation - math problem game

//change * 4 if you want more operators then add the
//operator as a string inside the operator array...
//you may also add duplicate operators to increase odds
//higher odds of harder operator = harder mode
var a = location;
function start() {
document.body.innerHTML = null;
var operators = ["/", "*", "-", "+"],
arr = ,
arrCompiled,
i = 0, a = location;
while (arr.length < 8) {
var r = Math.floor(Math.random() * 100) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
while(i < arr.length) {
var oper = ' '+operators[Math.floor(Math.random() * 4)]+' ';
if (i === 0) {
arrCompiled = arr.toString()
.replace(/,/, oper);
}

arrCompiled = arrCompiled.replace(/,/, oper);
i++;
}
document.write('<div id="questions">'+arrCompiled+' = ???</div>');
document.getElementById("questions").insertAdjacentHTML('afterend', '<button id="solve">Solve</button>');
}
start();

document.getElementById('solve').addEventListener('click', function() {
if (document.getElementById('solve')) {
var x = document.getElementById('questions').innerText.replace(/?/g, "");
var y = x.replace(/[^0-9*/+-.()]+/g, "");
var z = Math.round(eval(y) * 100) / 100;
document.write(x + z + ' - solved <a href="javascript:a.reload();"><button>New Problem</button></a>');
document.getElementById('questions').remove();
}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 27 '18 at 6:05









Techtiger255 Code ModeTechtiger255 Code Mode

4511




4511













  • Thank you for the your answer my friend.

    – siaylum
    Nov 28 '18 at 2:27



















  • Thank you for the your answer my friend.

    – siaylum
    Nov 28 '18 at 2:27

















Thank you for the your answer my friend.

– siaylum
Nov 28 '18 at 2:27





Thank you for the your answer my friend.

– siaylum
Nov 28 '18 at 2:27


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53492182%2fjavascript-to-generate-the-questions-using-random-numbers-using-select-operators%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks