0

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
        //}
Jake
  • 1
  • 3
    You're looking for [`indexOf()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf) – Paul Roub May 21 '15 at 16:08

2 Answers2

1

EDIT: If you're looking for a pragmatic solution, use indexOf as suggested in the comments. If you're looking for the proper algorithmic way of doing it (that won't require linear lookup-time and result in an asymptotically faster algorithm, the follow my original advice below:

Store the number as the key in an object.

var myNumbers = {};

...

myNumbers[mynum] = true; // add it to the object

if (myNumbers[mynum]) {
  console.log("The number has already been added");
} else {
  console.log("This is a new number");
}
Jakob
  • 24,154
  • 8
  • 46
  • 57
  • 1
    This is almost it, but it only should check if key exists, because it would not work if `0` has been encountered – axelduch May 21 '15 at 16:10
1

Here is how to you could test if mynum is already in myarray and change the color accordingly:

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++){



            var min = 1;
            var max = 20;
            var randomnum = Math.floor(Math.random() * (max - min + 1)) + min;
            var mynum = parseInt (randomnum );
            var color="black";
            if(myarray.indexOf(mynum) > -1){
            color="red";
            }       
            myarray[i] = mynum;


        document.writeln("<tr>");
        document.writeln("<td>" + i + "</td>");
        document.writeln("<td><font color="+color+">" + myarray[i] + "</td>");
        document.writeln("</tr>");
        }

        document.writeln("</table>");
WilliamSF
  • 284
  • 1
  • 8