jeudi 21 mai 2015

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("<table>");
        document.writeln("<th> Index </th>");
        document.writeln("<th> Number </th>");

        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("<tr>");
        document.writeln("<td>" + i + "</td>");
        document.writeln("<td>" + myarray[i] + "</td>");
        document.writeln("</tr>");
        }

        document.writeln("</table>");

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




Aucun commentaire:

Enregistrer un commentaire