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

Jeffrey Frye
Jeffrey Frye
5,385 Points

Why is my function not working?

Hello,

I'm not sure why my code is not working. Can you help me?

script.js
function max(number1, number2) {
  number1 = (Math.random() * 7);
  number2 = (Math.random() * 7);
  if (number1 > number2) {
    return number1; 
} else if (number1 < number2) {
    return number2
} else {
  return "error"
}
} 

5 Answers

Steven Parker
Steven Parker
231,236 Points

You're working way to hard! Your function:

  • won't do anything "random"
  • won't need to perform any math
  • only needs to return one of the arguments

Also, it's not an error if the values are the same, in that case you can return either one.

Jeffrey Frye
Jeffrey Frye
5,385 Points

Thanks man, I'm still kind of lost though

Steven Parker
Steven Parker
231,236 Points

If you still have trouble, show your updated code after you've applied those hints.

Jeffrey Frye
Jeffrey Frye
5,385 Points

function max(3,5) { if (3<5) { return 5; } else { return 3; } }

Steven Parker
Steven Parker
231,236 Points

You're close, but you changed a bit too much. You can't use numbers as function parameter names. But if you keep the structure you have now, and go back to the names you had before ("number1", etc) you should pass task 1.

Jeffrey Frye
Jeffrey Frye
5,385 Points

Final code -- Still not working

function max(number1, number2) { number1 = 10; number2 - 3; if (number1 > number2) { return number1; } else { return number2; } }

Could you help me understand why? Its probably a silly mistake. Nevertheless, very frustrating.

Steven Parker
Steven Parker
231,236 Points

Now you've added in an assignment and some math (subtraction).

What I was suggesting is that you keep structure you had before, and only change the "3"s to "number1" and the "5"s to "number2".

Jeffrey Frye
Jeffrey Frye
5,385 Points

I appreciate the help lol