javascript - Why is this 2d array pushing values when it shouldn't? -
edited: pointing out oblivious mistake temprndnumber being reset in inner loop. i'm still seeing "," characters show in array though.
i want create 2d array gets populated when random number meets particular criteria (rnd >= 7). following code populates 2d array combination of "," , numbers meets criteria.
var tempallrndnumbers = []; (var = 0; < 10; i++) { (var j = 0; j < 10; j++) { var temprndnumber = []; var rndnumber = math.floor(math.random() * 10); if (rndnumber >= 7) { temprndnumber.push(rndnumber); } } tempallrndnumbers.push(temprndnumber); }
tempallrndnumbers should populated numbers 7 , above, right? instead, i'm getting 2d array full of "," , numbers 7 , above.
since reset temprndnumber
empty array on each iteration of j
loop, contain number if last iteration >= 7. move initialization outside of innermost loop:
var tempallrndnumbers = []; (var = 0; < 10; i++) { var temprndnumber = []; (var j = 0; j < 10; { var rndnumber = math.floor(math.random() * 10); if (rndnumber >= 7) { temprndnumber.push(rndnumber); } } tempallrndnumbers.push(temprndnumber); }
Comments
Post a Comment