dimanche 8 février 2015

How do I create a list of random numbers using JavaScript?

My code is working but it isn't producing the output I want; I have three lists. One produces all random numbers, second produces the the numbers that are even, and the third produces numbers that are odd. Everything seems fine but the third list is only producing one odd numbered element instead of the other odd numbers the first list generates. How can I fix this?


Javascript:



var nums = [];

var allLists = "<ul>";
var evenList = "<ul>";
var oddList = "<ul>";

for(i = 0; i < 50; i++){
nums[i] = parseInt(Math.random() * 10);
allLists += "<li>" + nums[i] + "</li>";
if(nums[i] % 2 == 0){
evenList += "<li>" + nums[i] + "</li>";
}
else{
oddList = "<li>" + nums[i] + "</li>";
}
}

allLists += "</ul>";
evenList += "</ul>";
oddList += "</ul>";

document.getElementById("div1").innerHTML = allLists;
document.getElementById("div2").innerHTML = evenList;
document.getElementById("div3").innerHTML = oddList;


HTML:



<div id="div1">
</div>

<div id="div2">
</div>

<div id="div3">
</div>

Aucun commentaire:

Enregistrer un commentaire