card memory game in javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hicksmcb
    New Member
    • Nov 2006
    • 6

    card memory game in javascript

    Hi i'm having some trouble with a card memory game. Typical type. You turn over one card then another, if they dont match you turn them both back over and start again.

    I've got this 'if else' loop and the only way i can get the 'count number of turns' to work is by having the variables inside the 'if' statement which doesn't seem right to me.

    My main concern though is right at the very bottom of the code, i've tried to take the SCORE variable and see if it is greater than HIGH SCORE, if so HIGH SCORE becomes OLD SCORE and SCORE becomes HIGH SCORE.
    For some reason it keeps coming up with error, object expected. I've checked for spelling mistakes and the usual stuff but cant work out whats going on.

    Any help muchely appreciated.

    Code:
    function turnOver(e)
    {
      if (matchCount<8 & !matched[e.name])
      {	
    
    	if (clicked<1)
    	{
    	  firstCard=e; //grab first card object
    	  pair=cards[firstCard.name]; //get first pair number from cards array
    	  e.src=document.images["load"+pair].src; //flip over first card
    	  clicked++;
        } 
        else
        {
    	  e.src=document.images["load"+cards[e.name]].src; //flip over second card
    	  if (pair!==cards[e.name])
    	  { //check for snap
    		alert("That's not a match, try again!");
    		firstCard.src=document.images["load0"].src; //flip first card back
    		e.src=document.images["load0"].src; //flip second card back
    		count1 ++;
    		
    	  }
    	  else
    	  {
    		//This is a match so record matched cards and increase matchCount
    		matched[firstCard.name]=true;
    		matched[e.name]=true;
    		matchCount++;		
    	  }	  
    	  clicked=0;  
    	}
      }
    var plus = (matchCount*5);
    var minus = count1;
    var score = (plus - minus);
    if (matchCount > 7)
      {
      alert("Congratulations you matched "+matchCount+" pairs, for "+plus+" points with "+minus+" misses, giving you " +score+ " overall");
      }
      
    }   
    
    var high score=0;
    var old score=0;
    if (score > high score)
    	{
    	old score = high score;
    	high score = score; 
    	alert("gfag "+high score+" gfgfg");
    	}
    Last edited by acoder; Feb 1 '07, 09:50 AM. Reason: Code in tags
  • duye
    New Member
    • Jan 2007
    • 10

    #2
    Originally posted by hicksmcb

    var high score=0;
    var old score=0;
    if (score > high score)
    {
    old score = high score;
    high score = score;
    alert("gfag "+high score+" gfgfg");
    }
    var high score=0; do you have a space between high and score? space is not allowed for naming a variable.

    try this highScore oldScore.

    Comment

    • hicksmcb
      New Member
      • Nov 2006
      • 6

      #3
      Originally posted by duye
      var high score=0; do you have a space between high and score? space is not allowed for naming a variable.

      try this highScore oldScore.
      Tried that, didnt work. My main concern is why i have to name my variables inside the loop to get them to work

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by hicksmcb
        Tried that, didnt work. My main concern is why i have to name my variables inside the loop to get them to work
        Did you change the bottom code to:
        Code:
        var highScore=0;
        var oldScore=0;
        if (score > highScore)
        	{
        	oldScore = highScore;
        	highScore = score; 
        	alert("gfag "+highScore+" gfgfg");
        	}
        As Duye said, a variable cannot contain spaces.

        As to your second query, you don't have to "name" variables (I assume by that you mean declare them), but you must use them if you are going to maintain a count.

        You could declare global variables outside the function, set them to 0 to begin with, then use them freely within the function. Sometimes, you may need to set values to 0 to begin a loop.

        Hope that helps. If I've misunderstood or you need a better explanation, please post again. Perhaps you could give examples of which variables you are referring to.

        Comment

        • hicksmcb
          New Member
          • Nov 2006
          • 6

          #5
          Yeah sorry i meant 'declare'. I tried to set oldscore, highscore etc as global variables but for some reason it was as if the loop didnt recognise them and i kept getting a '0' value or a not declared value back for the calculations from the loop. I was reading up on functions.
          I did try taking away the space for the variables and it still doesnt work. Thanks for your help anyway. If you can think of anything else it would be appreciated.
          I'm not very good with if statements so it's probably that i've put the code inside the wrong curly brackets or something.

          Originally posted by acoder
          Did you change the bottom code to:
          Code:
          var highScore=0;
          var oldScore=0;
          if (score > highScore)
          	{
          	oldScore = highScore;
          	highScore = score; 
          	alert("gfag "+highScore+" gfgfg");
          	}
          As Duye said, a variable cannot contain spaces.

          As to your second query, you don't have to "name" variables (I assume by that you mean declare them), but you must use them if you are going to maintain a count.

          You could declare global variables outside the function, set them to 0 to begin with, then use them freely within the function. Sometimes, you may need to set values to 0 to begin a loop.

          Hope that helps. If I've misunderstood or you need a better explanation, please post again. Perhaps you could give examples of which variables you are referring to.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            What do you mean by 'loop'? I can't see any loops in the code that you've posted.

            Comment

            • UniDyne
              New Member
              • Oct 2005
              • 18

              #7
              I think the bit of code where you check the highScore and the current score should go in your function right after you display the Congrats message. Checking the scores outside of that would be pointless because you just declared them as zero and they haven't changed yet!

              Comment

              Working...