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

bevinwg
bevinwg
5,196 Points

Create a max() Function quiz: I've tried this about 15 different ways and I canNOT seem to figure this out

"Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two."

I have tried by starting to name the Function getMax and then name the var max. I have tried by using (5||9) in the solution. I have tried by using if, then statements. I have tried using variables (x,y) instead of numbers. I have even looked up the Math.max function on the MDN. I am 100% sure I am over complicating this somehow. I just can't seem to get this right, no matter how I try this. Any clues?

script.js
function max(5,9){
  var max= (5||9);
  Math.max (5,9);
  return max;
}  
script.js
function max (a,b) {
 if (a<b) {
    return b;
}
   else {
   return a;
}

3 Answers

bevinwg
bevinwg
5,196 Points

Thanks Michael. Yes, I am WAYYY over complicating it.

You're welcome. Don't forget to just walk away from it once in a while.

bevinwg
bevinwg
5,196 Points

Going to have to turn it off.
It won't take that type of response either. And nothing in my notes looks remotely like what they seem to be looking for. I'm supposed to be using variables instead of actual numbers, right? I tried both ways and keep getting the same syntax type or "Parse" errors.

also I am frequently getting these kick out messages during the quizzes that say that treehouse keeps losing where I am and is restarting the code challenges... tomorrow is another day.

odd, that code ran for me. Yes, variables would be correct as the arguments here. The next part of that challenge will ask you to call it and return. As for the messages I get that all the time. Don't click on "retry" or whatever it says. click outside the box in the code editor, it will dismiss the alert and you can click "check the code" again. It usually works on the 2nd attempt. Sometimes the 3rd.

bevinwg
bevinwg
5,196 Points

Thank you Michael. Appreciate the assistance. Was told it was related to Plug-Ins in the browser. I run Safari though they say they prefer I run chrome to not have the issues.