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 JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Why am I getting a parse error?

var firstNumber = prompt("Please enter a number"); var secondNumber = prompt("Please enter another number"); firstNumber = parseInt(firstNumber); secondNumber = parseInt(secondNumber); function max(firstNumber, secondNumber) { If (firstNumber > secondNumber) { var result = firstNumber; return result;

} else if (firstNumber < secondNumber) { var result = secondNumber; return result; } else { alert("Your numbers are equal"); } } alert( result + " is the greater of the two numbers");

script.js
var firstNumber = prompt("Please enter a number");
var secondNumber = prompt("Please enter another number");
firstNumber = parseInt(firstNumber);
secondNumber = parseInt(secondNumber);
function max(firstNumber, secondNumber) {
  If (firstNumber > secondNumber) {
    var result = firstNumber;
    return result;

  }
 else if (firstNumber < secondNumber) {
    var result = secondNumber;
     return result;
  }
  else {
    alert("Your numbers are equal");
}
}
  alert( result + " is the greater of the two numbers");
Katherine Ebel
Katherine Ebel
43,799 Points

Hi jeffkorfanta98. Change If to if in your max function, and see if that helps.

Thanks, but I'm still getting a parse error.

Now I'm getting the following error message: "ReferenceError: Can't find variable: result

I put the alert statement inside the function and now it works!

Katherine Ebel
Katherine Ebel
43,799 Points

Yes because since you defined result inside your function, you cannot access it outside of the scope of that function. You want to alert what is returned by calling that function.

2 Answers

Katherine Ebel
Katherine Ebel
43,799 Points

I think you are getting the Reference Error because you are accessing the result variable outside of the function when you call the alert method. Move the last call to alert inside of the max function, and I think that should work... In the challenge you want to alert the result of calling your max function (for the second step).

yes.. this is an issue as well.

If

uncap that i