javascript - memory game in jQuery save image in array -


i creating memory game using jquery, problem 1 specific thing, dont find wrong.

the way built it: images have default src value, after 1 click src change, if the 2 image clicked not same, default src value return.

the problem is: after defualt src value return, when click on image again, last shown src should retrive array, show antoher random image instead.

this relevent code

var imgarr1 = ["images/skinny-unicorn.png", "images/all-757448_640.jpg", "images/alm-770667_640.jpg", "images/audi-798530_640.jpg", "images/landscape-691462_640.jpg", "images/skinny-unicorn.png", "images/all-757448_640.jpg", "images/alm-770667_640.jpg", "images/audi-798530_640.jpg", "images/landscape-691462_640.jpg"];  var arrstr = [32];  $('.img').click(function(){     //var opencount = opencardcount();      var id = $(this).attr("id");     if(getfromarray(id) != 'null')     {         var oldsrc = getfromarray(id);         $('#' + id).attr('src',oldsrc);      }     else     {         var src = $(this).attr("src");         if(src == "images/back.png")         {              $('#' + id).attr('src',changefirstarray());         }         var opencard = opencardcount();         if(opencard == 1)         {             storeinarray(id,src);             $(".img").attr("src","images/back.png");         }     } });    /* create array of id , src been allready clicked once */ function storeinarray(id,src) {      for(var = 0 ; < arrstr.length; i++)     {         if(arrstr[i] == "")         {             arrstr[i] = id;             arrstr[i+1] = src;         }     }   }   /* check if there old image on id, return src if there one, if not return null */  function getfromarray(id) {     for(var = 0 ; < arrstr.length; i++)     {         if( id == arrstr[i])             var src = arrstr[i+1];         var src = 'null';     }      return src; } 

update getfromarray function, problem still exist

function getfromarray(id) {     for(var = 0 ; < arrstr.length; i++)     {         if( id == arrstr[i])             {               var src = arrstr[i+1];             }          else{var src = 'null';}              }      return src; } 

after line set

src = arrstr[i+1];  

you can add line

return src;  

as not need keep going through rest of loop

function getfromarray(id) {     for(var = 0 ; < arrstr.length; i++)     {         if( id == arrstr[i])             {               var src = arrstr[i+1];               return src; // inserted loop ends after find value after.             }          else{var src = 'null';}              }      return src; // occur if end of loop , value not found                 // returns null } 

to simplify function:

function getfromarray(id) {     for(var = 0 ; < arrstr.length; i++)     {         if( id == arrstr[i])             {               var src = arrstr[i+1];               return src;              }           }     return 'null';  } 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -