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

Nathaniel Boonzaaijer
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathaniel Boonzaaijer
Full Stack JavaScript Techdegree Student 8,607 Points

What is wrong with my code?

It says there is a syntax error, but I don't know where it is.

script.js
function max(number1, number2) {
  var number1 = prompt("number?")
  var number2 = prompt("number?")

  if number1 < number2 then,
    return (number2 + "is bigger.") 
    else, return (number1 + "is bigger.")

  }
max()

1 Answer

Matthew Long
Matthew Long
28,407 Points

You have a number of syntax errors in your code.

First, the parameters you pass in are the numbers that the function will use to determine which is greater. Therefore, you don't need to prompt the user for new numbers inside the function. Your if else statement lacks curly braces, and uses a then keyword that doesn't accomplish what you're wanting. Also, don't forget semicolons. Below is a correct solution, that is still close to yours, that this challenge is after:

function max(number1, number2) {
  if (number1 < number2) {
    return number2;
  } else {
    return number1;
  }
}

max(5, 8); // enter numbers here instead of prompting user inside your function

Also, pay attention to what the challenge is asking. For example, you tried returned a string "8 is bigger". But the challenge is only wanting a number to be returned.

Now you'll be able to go to the second part of this challenge!