mardi 24 mars 2015

Generating and checking random numbers in JavaScript

I have some problems with JavaScript. So far, I have this code:



<!DOCTYPE html>
<html>
<body>

<p>This program generates random numbers from 100 to 999.</p>

<button onclick="maingen()">Start</button> <-- Here's the FUNCTION OF FUNCTIONS..It should generate 3 digits, but only generates 1 :(

<p id="numbers"></p>

<script>
function generate1() {
var a = Math.floor((Math.random() * 9) + 1);
document.getElementById("numbers").innerHTML = a; <--generating the first digit (from 1 to 9)
}

function generate2() {
var b = Math.floor((Math.random() * 9) + 0);
document.getElementById("numbers").innerHTML = b; <--generating the second digit (from 0 to 9)
}

function generate3() {
var c = Math.floor((Math.random() * 9) + 0);
document.getElementById("numbers").innerHTML = c; <--generating the third digit (from 0 to 9)
}
function maingen(){
generate1();
generate2();
generate3();
}


</script>

</body>
</html>


And it doesn't work like I intended to. It should generate a random number from 100 to 999.


(I am generating separate digits because later I will need to check if there are same digits in that number (for example 222)).


So what did I do wrong? Any kind of help would be nice. Thank you.





Aucun commentaire:

Enregistrer un commentaire