0

What is the best practice when counting the number of times an action has been carried out in javascript? for example I have a prompt that asks for a number

 var playerGuess = prompt("What is your guess ");

What i would like to do is after 3 attempts end the game with another prompt.

What I am having difficulty with is actually counting the number of inputs

Thanks

I have tried creating a function do count the number of times an input has been made

    var guessCount = playerGuess.count;


   function limit(playerGuess){

  if (guessCount >= 3){
   alert("game over");
  } else{
   alert("carry on");
  }

 }

totally wrong i know but having a go

Richlewis
  • 15,070
  • 37
  • 122
  • 283

3 Answers3

1

You should evaluate the answer you get each time. If the answer is valid, take the count in another variable and when the count reaches the desired amount take no inputs.

var attempts    = 0;

function ask_question(){

  if(attempts > 3)
  {
   // you have played enough!
   return;
  }
  else
  {

   var playerGuess = prompt("What is your guess ");

   if(parseInt(playerGuess) != NaN && playerGuess != '')
   {
    attempts++;
    // do whatever you would like to do with playerGuess
   }
  }
}
Ahmet Özışık
  • 443
  • 3
  • 11
1

Like so:

// Global var to hold number of guesses
var guessCount = 0;

// Function to get the guess
function getGuess() {
  // Get a new guess
  var guess = prompt('What is your guess ');

  // Process guess here, eg:
  if (...whatever tests you want to make...) {
    // Good guess
    alert('Good guess: ' + guess);

  } else {
    // Bad guess
    guessCount += 1;

    // Fail out if too many guesses have been tried
    if (guessCount >= 3) {
      alert('Game over');
      return;
    }
  }
};

Cheers!

Irongaze.com
  • 1,639
  • 16
  • 22
  • This will not work if the user enters something like "asdfasdfaf" whereas they were expected to enter a numerical input. Also it will count empty guesses. – Ahmet Özışık Oct 02 '12 at 14:09
  • Yup, but it is trivially modified to make any logic required by the questioner - the issue was counting tries. The solution is a global variable that is incremented for each attempt. I'll tweak the flow a bit to make that more obvious... – Irongaze.com Oct 02 '12 at 14:11
  • Well, the logic of the game is hitting a certain number in order to win. It is not hard to guess that the next step in the flow is comparing the answer with the number. I understand it is not directly asked but if the answer of the player is not even a number there might have been an accident with the keyboard or something :) – Ahmet Özışık Oct 02 '12 at 14:18
  • Given that this is likely someone's homework assignment (ahem), didn't want to hand him the whole thing on a platter... ;-) – Irongaze.com Oct 02 '12 at 14:19
  • That is not my aim, neither. But never mind, there's always someone around who is willing to ;-) – Ahmet Özışık Oct 02 '12 at 14:22
  • thanks for your help, its not a homework assignment, im just starting to learn javascript and am building very simple projects – Richlewis Oct 02 '12 at 14:23
  • Well, good luck with it Rich! Javascript is pretty easy once you figure out scoping issues. Its power comes from closures: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work and from its inherent flexibility. But it does take some getting used to! – Irongaze.com Oct 02 '12 at 14:30
  • thanks, nice link by the way, wouldnt it be great if everything could be explained to a 6 year old :) – Richlewis Oct 02 '12 at 14:39
1

You could do this with a while loop and a variable to store the current iteration. Consider the following, which gives you three chances to guess the "secret" number:

var secretNumber = 42,
    youWon = false,
    i = 0;

while (i < 3) {
  var playerGuess = prompt("What is your guess?");

  if (playerGuess == secretNumber){
    youWon = true;
    break;
  }

  i++;
}

if (youWon) {
  alert("You got it!");
} else {
  alert("Sorry, you have no more tries left.");
}

This code loops over and over, incrementing i each time. It asks the question, and checks the answer. If the answer is right, it sets the youWon flag and breaks out of the loop, ending it early. Otherwise, the loop ends naturally after 3 iterations. After the loop is done, the youWon flag is checked to determine if the loop ended because the right answer was given, or if it ended because the number of tries was exhausted.

smitelli
  • 6,835
  • 3
  • 31
  • 53