0

I have two sections of my code, each generate a random equation with two numbers and an operation. Example: 28=16 ------ 2(randomized number) "" (randomized operation symbol) 8 (randomized number) = 16 (result). One section of the code is to add and subtract, the other is to multiply and divide. I want to only display one of these options on the website (either add and subtract or multiply and divide). How can I choose to display only one of this two RANDOMLY. Here is the code.

'''
////////////add & subctract

const firstNumber = ((Math.random()*(10000))-5000).toFixed(2);
const secondNumber = ((Math.random()*(10000))-5000).toFixed(2);

const operations = ["+", "-"];
const randomOperation = operations[Math.floor(Math.random() * Math.floor(2))];


const expression = firstNumber + randomOperation + secondNumber;

const resultNotInterger = eval(expression);

const result = parseInt(resultNotInterger);



 ////////////multiply and divide

 const firstNumber = ((Math.random()*(1000))-500);
 const secondNumber = ((Math.random()*(1000))-500);

 const operations = ["*", "/"];
 const randomOperation = operations[Math.floor(Math.random() * Math.floor(2))];


 const expression = firstNumber + randomOperation + secondNumber;

 const resultNotInterger = eval(expression);

 const result = parseInt(resultNotInterger);

 ///this is my nodeJS in case you wanted to see it.

app.get("/", function(request, response){
response.render("index.ejs", {
firstNumber: firstNumber,
secondNumber: secondNumber,
randomOperation: randomOperation,
result: result
  });
});
 '''

1 Answers1

0

random of 1 equates to true, random of 0 equates to false; if true, variables define to +/- values, if false, variables define to *// values; the rest of the variables are unaffected since values are identical in both operations.

const random = Math.round(Math.random() * 1);

const firstNumber = random ? ((Math.random()*(10000))-5000).toFixed(2) : ((Math.random()*(1000))-500)
const secondNumber = random ? ((Math.random()*(10000))-5000).toFixed(2) : ((Math.random()*(1000))-500)
const operations = random ? ["+", "-"] : ["*", "/"]; 

const randomOperation = operations[Math.round(Math.random() * 1)];
const expression = firstNumber + randomOperation + secondNumber;
const resultNotInterger = eval(expression);
const result = parseInt(resultNotInterger);

Pavlos Karalis
  • 2,893
  • 1
  • 6
  • 14
  • Thank you so much! It did work. I've never seen that method used before, so just for me to learn, if you add ----Math.round(Math.random() * 8);---- does that mean I can have 9 different choices? or is it just true and false (0 and 1)? also, I have another problem, it is not part of this question, so you dont have to answer it, it is on this link https://stackoverflow.com/q/63025941/13801785 ----........---- Thank you again! –  Jul 22 '20 at 02:37
  • Math.round would yield 9 choices since it would fall between 0 and 8; Math.floor would yield 8 choices since it would yield between 0 and 7. 0 always equates to false in javascript; any integer equates to true. – Pavlos Karalis Jul 22 '20 at 02:43