Ask coding questions

← Back to all posts
Getting Highest Population and Lowest Population
tymergoss (0)

Keep getting a reference error stating "highestPopulation is not defined at run. The goal of the assignment is to use function printAndGetHighScore to output highest population and lowest population. My question is where do I define highestPopulation within my code?

Comments
hotnewtop
19wintersp (1121)

On line 72 of "census.1.js", you're referencing the variable highestPopulation, which is not defined in that scope. If you want to get the value computed and returned in the function printAndGetHighScore, you should call it (and pass the statePopulationList as a parameter):

console.log("highest population value is: " + printAndGetHighScore(statePopulationList));
tymergoss (0)

thank you, that got rid of the reference error. Right now the highest and lowest populations are not getting outputted correctly @19wintersp

19wintersp (1121)

@tymergoss You've got a couple of issues. The root cause is a bit counter-intuitive; try running this in a JavaScript console:

function main() {
  function f() {
    console.log("nope");
  }
  f();
  function f() {
    console.log("what");
  }
}
main();

You should see "what" printed out. This is because of a JavaScript feature (?) known as hoisting. This means that when you call printAndGetHighScore on line 72, you're actually calling the function defined on line 81. You can temporarily fix this to test it by commenting out the function starting on line 81.