-1

'm trying to create a function that will continuously loop through an array and check if there are still elements with a certain value. If there are no more of these elements, then I would like the function to execute a certain action.

I'm checking for '0'. If nothing is ='0', then i want to display an image. Here's what I have, any suggestions?

  function partiewin()
// On verifie si il y a encore des cases avec pour valeur '0' et si non, on     fini la partie
{

var found=false;
for (i=1; i <= hauteur; i++)
{
   for (j=1;j <= largeur; j++)
   { 
     if( decor[i][j]!=0)
     {
      window.alert("You win");
       found=1;
     }
   }
}
if(!found)
{

   }
   }

This is the array

  var decor = new Array(hauteur); 
for (i=0; i <= hauteur; i=i+1)
{
    decor[i] = new Array(largeur);
}

The array is a long list of this shape :

decor[1][1] = '24'; decor[1][2] = '21'; decor[4][8]='0' ; etc 

Shouldn't this work? I'm not getting any alerts or any answer whatsoever once all the '0' are technically gone from the map..

JuCho
  • 1
  • 2
  • 2
    Please clarify your expected and actual results. Also where are `hauteur` and `largeur` defined? You should provide complete, but precise, code examples. – Oka May 29 '15 at 19:18

1 Answers1

1
var found = false;
for(var i = 0; i < hauteur.length ; i++){
 for (var j = 0; j< hauteur[i].length ; j++){ 
    if( hauteur[i][j] == '0'){ 
    found = true;
    break;
    }
}
}

if(!found){

    console.log("display image code here")
}
Dusty
  • 354
  • 4
  • 15
  • i replaced console.log("display image code here") with window.alert("You win"); but it still shows no response? any ideas? – JuCho May 29 '15 at 19:22
  • I think you have a value of '0'. Try to add else to the if statement that will through an alert if there is a '0' value – Dusty May 29 '15 at 19:35