javascript - Get same values with Math.random -


in below code, want function 2 change text in text complete cycle. unfortunately var rand not change text being different word. rather remain same whole time.

var x = 0;  function function1() {     document.getelementbyid("text").textcontent = rand; }  function function2() {     if (document.getelementbyid("text").innerhtml === "red") {         x += 1;         document.getelementbyid("text2").textcontent = x;         function1();     } }  //i have got equivalent functions blue, green , gold    var randomarray = ['red', 'blue', 'green', 'gold', ]; var rand = randomarray[math.floor(math.random() * randomarray.length)]; 

can please show me how fix this.

move declaration of rand inside function1. way indeed chose again color each time function1 called

function function1() { var rand = randomarray[math.floor(math.random() * randomarray.length)]; document.getelementbyid("text").textcontent = rand; } 

and comment out (or remove) declaration have outside both functions becomes irrelevant.

//var rand = randomarray[math.floor(math.random() * randomarray.length)]; 

Comments