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

Help coding functions with conditional statements

Continue getting Syntax errors after endless editing. Appreciate any directional assistance.

script.js
function max (100,200){
  function addNumbers (100,200);}
return (100 + 200);
if (100 > 200);
document.write ("100 is bigger than 200");
}else {
  document.write ("100 is smaller than 200");}
}

2 Answers

Mary Mohlenkamp I am a little confused why you have the addNumbers() function in there. I will paste my answer to the coding challenge and explain it to help you better understand it.

function max(num1, num2) {
  if(num1 > num2) {
    return num1;
  }
  else {
    return num2;
  }
}

So when you are creating a function, you want to name your arguments as vague but relevant as possible, so I chose "num1" and "num2" because once this function is created you can actually pass any numbers you want to it to find out which one is the maximum number. So your choice of "100" and "200" wouldn't be ideal, you would actually use those numbers when you were making a call to the function (executing it). So here I created a function that accepts two numbers "num1" and "num2" and then inside the function I have an "if" statement that checks to see if num1 is greater than num2... and if it is, then it returns num1 a.k.a the max. If num1 is not greater than num2 then my function jumps to the else statement (because the if statements conditions were not met). The else statement merely contains a "return num2" statement because in this instance (as long as the numbers aren't equal) if num1 wasn't greater than num2 (the original if statement), then num2 has to be the max, so we're going to return num2. I hope this helps! Feel free to send a reply if you need further assistance.

Thanks for your help!