javascript - Go through an array and check for certain values? -
i'm trying create function continuously loop through array , check if there still elements value. if there no more of these elements, function execute action.
i'm checking 0
. if nothing = 0
want display image. here's have, suggestions?
function partiewin() { // on verifie si il y encore des cases avec pour valeur '0' et si non, on fini la partie var found = false; (i = 1; <= hauteur; i++) { (j = 1; j <= largeur; j++) { if (decor[i][j] != 0) { window.alert("you win"); found = 1; } } } if (!found) { } }
this array
var decor = new array(hauteur); (i = 0; <= hauteur; = + 1) { decor[i] = new array(largeur); }
the array long list of shape :
decor[1][1] = '24'; decor[1][2] = '21'; decor[4][8]='0' ; etc
shouldn't work? i'm not getting alerts or answer whatsoever once '0' technically gone map..
since using array, try foreach method of arrays: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/foreach
in end imagine you're wanting loop through nested array of arrays.
http://jsfiddle.net/tylersdesk/yg8ag0vq/
var arrayofarrays = [ [2,1,2,3,4,5], [2,1,2,1,4,5], [8,1,2,3,4,5], [4,1,2,3,4,5], [2,1,2,3,4,5], [2,1,2,3,0,5], ]; var flag = false; (i=0; i<arrayofarrays.length; i++) { arrayofarrays[i].foreach(function(value, index, array){ console.log(value); if (value === 0) { console.log('found value'); flag = true; } }); } if (flag) { //do work }
there might issue way declaring arrays. instance when use new array(numberofindexes) you're setting how many indexes there array, nut setting values.
so see this:
var hauteur = 40; var largeur = 20; var decor = new array(hauteur); (i=0; <= hauteur; i=i+1) { decor[i] = new array(largeur); } console.log(decor); console.log(decor[0][1]);
if hauteur , larguer integers creating array of arrays without values.
remember, if passing single value new array()
, length of array, not value. set value need new array(value1, value2, value3,)
read this: https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array
i'd suggest throwing in log statements code make sure working arrays , more arrays have values.
Comments
Post a Comment