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

Uncaught Reference Error

Hi all,

I seem to be running in to some sort of scope issue. I keep getting the following error message:

VM119:22 Uncaught ReferenceError: servicePercent is not defined at getServiceCharge (<anonymous>:22:35) at mainProgram (<anonymous>:9:3) at <anonymous>:34:1

servicePercent is however defined in the mainProgram function. The getServiceCharge() function is able to access the other global variables. I have no idea why it's throwing an error for this variable. Can anyone help?

function mainProgram() {
  //declarations
  var lastName;
  var checkAmount;
  var serviceCharge;
  var servicePercent = 0.02;

  initialize();
  getServiceCharge();
  finish();
}

function initialize() {
  lastName = prompt("Enter the customer's last name.");
}

function getServiceCharge() {
  while (lastName.toUpperCase() !== "ZZZ") {
    checkAmount = prompt("Enter the check amount in dollars and cents");
    checkAmount = parseFloat(checkAmount);

    serviceCharge = checkAmount * servicePercent + checkAmount;

    console.log(`The service charge for ${lastName} is $${serviceCharge.toFixed}.toFixed(2)`);

    lastName = prompt("Enter the customer's last name.");
  }//end while
}

function finish() {
  console.log("Program complete");
}

mainProgram();

Thanks, Sharina V.

I've been able to work around this problem by adding servicePercent as a parameter for getService charge, however, I don't understand why it's out of scope.

1 Answer

It is because you declared the variable in the mainProgram() function and it is only available within that function and passing as a parameter is one way to use it within the scope of the other function. Another way is to declare it in the global scope (outside of any function) sometimes referred to as cluttering the global scope and is generally not recommended.

Scope is a key concept to understand.

Please review the description section of MDN variable statement page

Hi Dave,

Although the variable wasn't declared in the global scope, the getServiceCharge function is able to access the other variables in the mainProgram. Why can the getServiceCharge function access unintitialized variables and string variables but not numeric variables?

Hi Sharina,

Pretty sure that it is because you are assigning values to undeclared variables for those other variables and if you review the link above it has the below paragraph:

"Assigning a value to an undeclared variable implicitly creates it as a global variable (it becomes a property of the global object) when the assignment is executed. "

I don't think you are accessing the same variables from mainProgram() - what happens if you give them a value in mainProgram() and then try to console.log() their values in getServiceCharge()?