ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

only active score on button click #206

Closed ghost closed 9 years ago

ghost commented 9 years ago

I'm trying to active the score button on click and only count the score if that button was clicked. If the button was not clicked the score would not be kept. I've had the score outside of the click and then it shows up whether you push the button or not and starts taking score. So then I put it in the click but then it only updates the score when you keep clicking the button. I'm not sure how to have it only active when the score button is kept and stay de-active when its not clicked.

function takeScore() {
  $("#button2").click(function(){
    $(".numberbox").html("score:"+ ' '+score )
  })

  function changeScore(){
      if (card1==card2){
          score++;
          $(".numberbox").html("score:"+ ' '+score )
      }
      if(card1 !== card2){
          score--;
          $(".numberbox").html("score:"+ ' '+score )
      }
      /* let them know they kinda suck*/
      if (score == -3){
         alert("Seriously tho? try a little harder :-(")
      }
   }
}
changeScore()
takeScore()
amaseda commented 9 years ago

Hey Alea, not sure I understand the issue 100%. Could you provide some more context?

That being said, are you getting any errors in the console? As this code example stands, you can't call changeScore( ) in its current position because it only exists inside the scope of takeScore( ).

ghost commented 9 years ago

What i'm trying to achieve is that when you click the score button it will start taking score of the game. But only when you click that button otherwise the score should not be taken. I've just been moving the changeScore() function around in hopes that maybe that would solve the issue.

RobertAKARobin commented 9 years ago

Hah, well, that's one way to do it. Where do you have var score?

RobertAKARobin commented 9 years ago

Better yet, could you push your code to Github so I can take a look at your whole code?

ghost commented 9 years ago

var score = 0 is in the global floating around waiting to be used

https://github.com/ScreenName4eva/memoryGame/tree/gh-pages

http://screenname4eva.github.io/memoryGame/

RobertAKARobin commented 9 years ago

I think you need to fix your indentation. It looks like changeScore is inside takeScore. Therefore, takeScore isn't available outside of changeScore. This will be easier to see if your indentation was consistent!

ghost commented 9 years ago

changeScore is inside takeScore....erm...maybe thats the issue?

RobertAKARobin commented 9 years ago

See previous comment! :)

ghost commented 9 years ago

but i'm calling takescore so wouldn't that in turn call changesscore? I had changescore() commented out before while i was testing. I still have the issue of the score not changing which is why I started putting changescore() all over the place

RobertAKARobin commented 9 years ago

Still fix the indentation!

ghost commented 9 years ago

I don't know what I would fix it to it's indenting in a way that makes sense to me

RobertAKARobin commented 9 years ago

Here's an example:

function takeScore() {
  $("#button2").click(function(){
    $(".numberbox").html("score:"+ ' '+score )

  })

  function changeScore(){
   if (card1==card2){
        score++;
        $(".numberbox").html("score:"+ ' '+score )
  }
    if(card1 !== card2){
        score--;
        $(".numberbox").html("score:"+ ' '+score )
}
    /* let them know they kinda suck*/
    if (score == -3){
         alert("Seriously tho? try a little harder :-(")
       }
     }

To...

function takeScore() {
  $("#button2").click(function(){
    $(".numberbox").html("score:"+ ' '+score )
  })

  function changeScore(){
    if (card1==card2){
      score++;
      $(".numberbox").html("score:"+ ' '+score )
    }
    if(card1 !== card2){
      score--;
      $(".numberbox").html("score:"+ ' '+score )
    }
    /* let them know they kinda suck*/
    if (score == -3){
      alert("Seriously tho? try a little harder :-(")
// this } was missing
    }
  }
}

This showed me that there was a missing curly bracket.

RobertAKARobin commented 9 years ago

Atom makes it easy! You can select multiple lines and hit Tab to indent them to the right, and Shift-Tab to un-indent them to the left.

RobertAKARobin commented 9 years ago

The way you submitted it initially in this issue was great! It looks like it just got mungled a bit when it went up to Github.