0

I am trying to make a hangman game and in the beginning the game asks the user for the word. Once it has the word it fills the word letters one by one in an array using a for loop. Unfortunately the last array element is always undefined for some reason. Code(JS):

for(i=0;i<word.length;i++)
{
    if(i == word.length - 1)
    {
        wordLettersLeft = word.length;
        $("#cEText").removeAttr("style");
        $(".characterEnter").removeAttr("style");
        $("#gBtn").text("Lopeta");
        gameStarted = true;
        pcArrayLength = pcArray.length;
        lives = 4;
        alert("Peli alkaa! Sinulla on " + lives + " yritystä jäljellä.");
        alert("Vihje: " + hint);
        alert("Sinulla on vielä " + wordLettersLeft + " arvattavaa kirjainta jäljellä.");
    }
    else
    {
        pcArray.push(word[i]);
    }
}
4
  • 2
    How to run this code ? May be because you are not pushing it in pcArray Commented Oct 8, 2015 at 10:59
  • Your if block runs when you're on the last letter of the word. But you only add to pcArray in the else block. Commented Oct 8, 2015 at 11:03
  • it undefiend just because you not add it to pcArray Commented Oct 8, 2015 at 11:03
  • 1
    Wont it be easier to just split the word in its letters by using .split('') ? Then you can just write all the code that happens at the end after you split the string into an array. Commented Oct 8, 2015 at 11:03

3 Answers 3

1

This has a very simple reason: you are not adding the last letter to your pcArray. Let's assume our word is 'bird' (because bird is a word). Your code would go through the letters b, i and r and push those to your array, so far so good. But when it hits the d, it is at position word.length-1 - because the array length starts at 1 but the index starts at 0. Your if statement prevents it from pushing the last letter to the word. There are two solutions:

First, you could simply remove the else statement:

for(i=0;i<word.length;i++){
    if(i == word.length - 1){
        wordLettersLeft = word.length;
        $("#cEText").removeAttr("style");
        $(".characterEnter").removeAttr("style");
        $("#gBtn").text("Lopeta");
        gameStarted = true;
        pcArrayLength = pcArray.length;
        lives = 4;
        alert("Peli alkaa! Sinulla on " + lives + " yritystä jäljellä.");
        alert("Vihje: " + hint);
        alert("Sinulla on vielä " + wordLettersLeft + " arvattavaa kirjainta jäljellä.");
    }
    pcArray.push(word[i]);
}

That will push every letter every time! However, There is a more efficient way:

  for(i=0;i<word.length;i++)
      pcArray.push(word[i]);
  wordLettersLeft = word.length;
  $("#cEText").removeAttr("style");
  $(".characterEnter").removeAttr("style");
  $("#gBtn").text("Lopeta");
  gameStarted = true;
  pcArrayLength = pcArray.length;
  lives = 4;
  alert("Peli alkaa! Sinulla on " + lives + " yritystä jäljellä.");
  alert("Vihje: " + hint);
  alert("Sinulla on vielä " + wordLettersLeft + " arvattavaa kirjainta jäljellä.");

This way you will only execute your initial code once instead of checking the if every time. You know your loop will only run for as long as there are letters, so why even bother with the if?

As @Shilly mentions in the comments, you can forego the for loop altogether by doing this:

pcArray = word.split('');
Sign up to request clarification or add additional context in comments.

Comments

1

Let's say someone's inputing the word "Test". For i === 3 which is the last iteration, you enter the if clause and you're missing out on the else clause, which is where you're doing:

pcArray.push(word[i]);

What you want is to do this anyway, regardless of whether you're in the if or the else, so you can just drop the else clause and do it anyway.

Comments

0

The last step of your loop does not execute pcArray.push(word[i]);. So obvioulsy, the last value is missing.

But if you want to convert a string into an array, just do this :

var pcArray = word.split('');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.