Question

HTML and JAVASCRIPT Write the function that:  Generates a random number using Math.random, saves it...

HTML and JAVASCRIPT

Write the function that:

 Generates a random number using Math.random, saves it using a variable and writes the

number generated (the value of the variable) into the first textbox. Note: Math.random()

generated a random number that is between 0 and 1.

 Rounds the random number that was saved in a variable using Math.round and saves it

using a variable and then writes the result into the second textbox. Note: Since the random

number is between 0 and 1 it will round to either zero or one.

 If the value of the variable, that contains the rounded number, is equal to zero replace the

image with the image of the heads side of a coin.

 Otherwise, if the value of the variable, that contains the rounded number, is equal to one

replace the image with the image of the tails side of the coin.

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

Following code can be used for this problem-

<!DOCTYPE html>
<html>
<body>

<p> Random Number between 0 and 1- </p>
<p id="random"></p>
<p> Rounded value- </p>
<p id="round"></p>
<img id="Image" src="image.gif" style="width:100px">


<script>
var a = Math.random();
var b = Math.round(a);
document.getElementById("random").innerHTML = a;
document.getElementById("round").innerHTML = b;
if(b==0){
document.getElementById('Image').src='head.gif';
}
else{
document.getElementById('Image').src='tails.gif';
}


</script>

</body>
</html>

Use real source address for images to get the actual results.

Add a comment
Know the answer?
Add Answer to:
HTML and JAVASCRIPT Write the function that:  Generates a random number using Math.random, saves it...
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