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

Java script passing 2 arguments into a function to determine which number is larger using a if/else statement.

I have tried to get this and it is just stumping me. I bet it is something that is simple.

  1. I have created a max function. I am also trying to pass in two arguments which are numbers to determine which one is larger.

  2. I am using a if/else statement to determine which on is larger. I think maybe this is where I have messed up it looks like it can not determine the parameters that are being passed to it.

  3. I am trying to make everything return to the variable largerNumber and be printed to the console.

I believe I have used the correct terminology but if I have not please correct me so that I will not make this mistake in the future.

Thank you

script.js
function max(a,b){
  var largerNumber;


  if(a > b) {
    a = largerNumber;
  }else {
    b = largerNumber;

    return largerNumber;
  }

}

console.log(max(12, 19));

2 Answers

This issue in your code is assigning the large number to the variable. This should be reversed. The variable should be assigned to largeno and then returned.

Can you try like this. It works.

function max(a,b) {
  if (a > b)
  {
    return a;
  }
  else 
  {
    return b;
}
}

Thank you, I was over complicating things. The code you provided did the job.

I did not need to have a extra variable ( var largerNumber) to pass the results into.

Julian Aramburu
Julian Aramburu
11,368 Points

Hi Terrell! In order to assing a or b to largerNumber you should do largerNumber = a/b !!! You could also instead of using a variable only return the a or b argument that turns out to be the larger one.

Cheers!

thank you for replying. I was not writing my code efficiently. I just needed to take the extra variable out of the equations. Just return the what is needed.