Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Scope issue in JS

I have functions that all work independently, but when matched with blueprint code they do not run the hangman game properly. Im creating a beginner hangman game, using functions. To finish the game i was given a blueprint code to run the functions that I write, when I run the functions without the blueprint they all do as designed but when I uncomment the blueprint code I get the following error.I dont understand why it cannot access answerArray within the function as the variable is setup globally.

Uncaught TypeError: Cannot read property 'join' of undefined at showPlayerProgress (hangmannofunc.html:27) at hangmannofunc.html:48

function pickWord() {
  var words = [
    "marmalade",
    "moose",
    "juice",
    "goose",
    "fruit",
    "bed",
    "amazing"
  ];
    return words[Math.floor(Math.random()* words.length)];
}
function setupAnswerArray(word) {
  answerArray = [];
  for (var i = 0; i < word.length;i++) {
          answerArray[i] = " _ ";
  }
}

function showPlayerProgress(answerArray) {
  answerArray;
  alert(answerArray.join(" "));
}


function getGuess() {
  return guess = prompt("Guess a letter!");
}

function updateGameState(answerArray, guess, word) {
  for (var j = 0; j < word.length; j++) {
    if (word[j] === guess) {
       answerArray[j] = guess;
       remainingLetters--;
  }
}
}
function showAnswerAndCongratulatePlayer(answerArray) {
  alert("Conratulations, You did it! " + word + " was the correct answer!")
}
var word = pickWord();
var answerArray = setupAnswerArray(word); var remainingLetters = word.length;
while (remainingLetters > 0) { showPlayerProgress(answerArray); var guess = getGuess();
if (guess === null) {
break;
} else if (guess.length !== 1) {
alert("Please enter a single letter.");
} else {
var correctGuesses = updateGameState(guess, word, answerArray);
  remainingLetters -= correctGuesses; }
} showAnswerAndCongratulatePlayer(answerArray);
Steven Parker
Steven Parker
243,318 Points

Screenshots aren't very useful for analyzing code issues. But you could make a snapshot of your workspace and post the link to it here.

If you're not using workspaces, you can post code directly by using the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.


UPDATE: much better with the code included!

2 Answers

Steven Parker
Steven Parker
243,318 Points

The variable answerArray is a global first assigned by an implicit global reference at line 14 inside the setupAnswerArray function, but then becomes an undefined scalar (because nothing is returned) when the result of calling setupAnswerArray is assigned to it on line 42 in the blueprint code.

You probably want to create a local (and/or differently-named named) variable in the setupAnswerArray function, and then return it at the end of the function:

function setupAnswerArray(word) {
  var answerArray = [];  // adding "var" keeps it within the function scope
  for (var i = 0; i < word.length;i++) {
          answerArray[i] = " _ ";
  }
  return answerArray;    // the return value allows the assignment to work
}

There are still some other issues to resolve, but fixing this should get you going again.

answerArray is scoped to setupAnswerArray.

JavaScript uses function scope to scope its variables, etc. You cannot access answerArray inside setupAnswerArray. You'd either need to make it global or redeclare it which would be bad.

Steven Parker
Steven Parker
243,318 Points

It's actually a global, due to variable hoisting, and not scoped inside the function. See my answer for details.