Thursday, 8 June 2017

Javascript function to determine if array element already exists

So I'm writing some code that will generate a random number between 1 and 20 ten times, add it to an array, then display it in a table. If a number occurs more than once, it will display red in the table. I'm having trouble creating the function that would evaluate the random number to determine if it is random or not and turn it red. Any help is greatly appreciated



var i;
var myarray = new Array();



document.writeln("");

document.writeln("");
document.writeln("");

for (i = 1; i <= 10; i++){

//numberExists();

var min = 1;
var max = 20;
var randomnum = Math.floor(Math.random() * (max - min + 1)) + min;

var mynum = parseInt (randomnum );

myarray[i] = mynum;


document.writeln("");
document.writeln("");
document.writeln("");
document.writeln("");
}


document.writeln("
Index Number
" + i + "" + myarray[i] + "
");

//function numberExists(mynum, myarray){
// Can't figure out the code that goes here
//}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...