mardi 13 janvier 2015

Using function to display random numbers, then ifelse statement to allocate days of the week

when user clicks on button, the script should generate a random number and then display the corresponding week day. The code below is what I have so far, alas it doesn't seem to work. I have debugged it and apparently myfunction is not defined?!



<!DOCTYPE html>
<html>

<body>

<p>Click the button to display a random number between 1 and 7</p>

<button onclick="myFunction()">Get Random</button>

<p id="task1"></p>

<script>
function myFunction() {
var x = Math.floor((Math.random() * 7 + 1);
document.getElementById("task1").innerHTML = x;
}

if (x===1)
{
document.write("today is sunday");
}
else if(x===2)
{
document.write("today is monday");
}
else if(x===3)
{
document.write("today is tuesday");
}
else if(x===4)
{
document.write("today is wednesday");
}
else if(x===5)
{
document.write("today is thursday");
}
else if(x===6)
{
document.write("today is friday");
}
else if(x===7)
{
document.write("today is saturday");
}

</script>

</body>

</html>

Aucun commentaire:

Enregistrer un commentaire