JavaScript to generate the questions using random numbers using Select operators randomly(+,*,-) for each...
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);
javascript
add a comment |
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);
javascript
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
add a comment |
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);
javascript
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
javascript
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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}`}`)
});
Thank you for your answer my friend, how to add 2 marks per correct answer).
– siaylum
Nov 27 '18 at 4:09
add a comment |
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;
}
}
How to show this equation inprompt
. I wrote like this, but this is not working properly.eval(prompt (" + "));
– siaylum
Nov 27 '18 at 3:57
Edited my answer to useeval(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
add a comment |
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();
}
});
Thank you for the your answer my friend.
– siaylum
Nov 28 '18 at 2:27
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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}`}`)
});
Thank you for your answer my friend, how to add 2 marks per correct answer).
– siaylum
Nov 27 '18 at 4:09
add a comment |
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}`}`)
});
Thank you for your answer my friend, how to add 2 marks per correct answer).
– siaylum
Nov 27 '18 at 4:09
add a comment |
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}`}`)
});
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}`}`)
});
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
add a comment |
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
add a comment |
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;
}
}
How to show this equation inprompt
. I wrote like this, but this is not working properly.eval(prompt (" + "));
– siaylum
Nov 27 '18 at 3:57
Edited my answer to useeval(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
add a comment |
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;
}
}
How to show this equation inprompt
. I wrote like this, but this is not working properly.eval(prompt (" + "));
– siaylum
Nov 27 '18 at 3:57
Edited my answer to useeval(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
add a comment |
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;
}
}
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;
}
}
edited Nov 27 '18 at 4:00
answered Nov 27 '18 at 3:29
AndreasAndreas
1,97931018
1,97931018
How to show this equation inprompt
. I wrote like this, but this is not working properly.eval(prompt (" + "));
– siaylum
Nov 27 '18 at 3:57
Edited my answer to useeval(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
add a comment |
How to show this equation inprompt
. I wrote like this, but this is not working properly.eval(prompt (" + "));
– siaylum
Nov 27 '18 at 3:57
Edited my answer to useeval(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
add a comment |
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();
}
});
Thank you for the your answer my friend.
– siaylum
Nov 28 '18 at 2:27
add a comment |
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();
}
});
Thank you for the your answer my friend.
– siaylum
Nov 28 '18 at 2:27
add a comment |
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();
}
});
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();
}
});
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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