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

muna alkhalil
muna alkhalil
2,798 Points

I am having difficulties understanding (Functions and returns) I can't even do this simple test :( its very confusing!!!

I need help with this

function max (low,high){
  if (low < high)
    return true;
    return false;
var returnBigNumber = max(10,20);

3 Answers

Don't give up!

You are close!

You might've misunderstood the challenge. The function should take two numbers, and should return the bigger of the two.

For example...

max(1, 2);  // Should return 2
max(10, 12);  // Should return 12
max(5, 2);  // Should return 5
max(100, 70);  // Should return 100

// And so on...

Your function is taking two numbers, great, but it is returning a boolean! Did the challenge ask for that?

:point_right: Also, you don't need to call the function. The code challenge will do that automatically for you.

Here's a little hint, in case you get stuck:

function max(a, b) {
    // If a is greater then b...
        // Return a
    // Otherwise...
        // Return b

This isn't the code that will pass the challenge, but it may help you out.

I hope this helps :grin:

Happy C0D1NG! :tada:

:dizzy: ~Alex :dizzy:

Zachary Kaufman
Zachary Kaufman
1,463 Points

Alright below is a solution that worked for me (for task 1 of the challenge) but let me explain what you have to do first. If you can pass the challenge without using the solution below that's great! So earlier in the course you made variables (for example, a variable called name) and assigned it to whatever the user says by using alert(). Later in your code you know that you can use the name variable for whatever you might need it for, and it will keep the value of whatever the user said in the alert(). In the same way a function also has a value. The value that the function keeps is whatever follows the keyword "return". So when the challenge asked you to return the bigger of the two numbers, it is asking you to make sure that whenever you use the max function later in your code, that the value that max has inside of it is the larger of the two numbers. So in your code you have low = 10, and high = 20. Which means the "if" part of the code is true because 10 is < 20. The problem is, you returned the boolean true and not the lower number. So what you should have the possible return statement be is

if (low < high) {
    return low;
} else {
   return high;

you also don't need the variable at the end of your code, the website will plug random numbers into the low and high values to make sure that your function works. Does this make sense?

function max (num1, num2) {
  if (num1 < num2) {
    return num2; 
  } else {
    return num1; 
muna alkhalil
muna alkhalil
2,798 Points

ohhh thank you so much guys this really helped a lot. maybe I was writing more than what they asked me to. yeah sometimes I overthink and make it more complicated than what it really is!!! thanks :D

No problem :smile:

To mark this question as answered, can you give either Zachary or I a best answer? Thank you :)