Question

I need to create a bit of code for javascript which I can have random numbers...

I need to create a bit of code for javascript which I can have random numbers generate with a certain amount of digits set by the user. and ask the user how many times they want this to happen.
For example The user says they want a number with 4 digits (any number between 0 - 9999) and the user can input if they want to add, subtract, multiply, divide, or make it random
2 random numbers are generated. First number 1451 and the next number is 58.
If the user selects "random" the program will pick between the 4 (add multiple subtract divide)
Once the numbers are made 1451 & 58 the user then needs to "add" (random pick) and the profram lets them know if they did it correct or not.

Say the player select they want to do this 3 times. it just repeats the process above.
Additionally would it be possible to record the results?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code -

<!DOCTYPE html>

<html>

<title>Web Page Design</title>

<head>

<script>

var output,num1,num2,operator;

function myFunction() {

var digitCount = parseInt(document.getElementById("digit").value);

var numberRange = Math.pow(10, digitCount)

num1 = Math.floor(Math.random() * (numberRange - 1));

num2 = Math.floor(Math.random() * (numberRange - 1));

var x = parseInt(document.getElementById("mySelect").value);

var choice;

if (x == 6) {

choice = Math.floor(Math.random() * 4) + 2;

}

else {

choice = parseInt(document.getElementById("mySelect").value);

}

operator = getOperator(choice);

document.getElementById("demo").innerHTML = "First number " + num1 + " Second number " + num2 + " your choice " + operator;


}

function getResult() {

output = parseInt(document.getElementById("output").value);

var result = operation(num1,num2,operator);

if(result==output){

document.getElementById("result").innerHTML = "Correct answer";

}

else{

document.getElementById("result").innerHTML = "Wrong answer";

}

}

function operation(num1,num2,operator){

switch (operator) {

case '+': return num1+num2; break;

case '-': return num1-num2; break;

case '*': return num1*num2; break;

case '/': return num1/num2; break;

}

}

function getOperator(choice) {

switch (choice) {

case 2: return '+'; break;

case 3: return '-'; break;

case 4: return '*'; break;

case 5: return '/'; break;

}

}

</script>

</head>

<body>

Enter the number of digits <input type="text" id="digit"><br>

Select the operation you want to perform<select id="mySelect" onchange="myFunction()">

<option value="1">---Default---</option>

<option value="6">Random</option>

<option value="2">Add</option>

<option value="3">Subtract</option>

<option value="4">Multiply</option>

<option value="5">Divide</option>

</select>

<div id="demo"></div>

Your output : <input type="text" id="output">

<input type="button" value="Submit" onclick="getResult()">

<div id="result"></div>

</body>

</html>

Screenshots -

Add a comment
Know the answer?
Add Answer to:
I need to create a bit of code for javascript which I can have random numbers...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT