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

Ayoade Onafowora
Ayoade Onafowora
2,762 Points

Please whats wrong with my code?

Kindly help...

script.js
function max (20, 10) {
  if (20 > 10) {
    return 20;    
  } else {
    return 10;
  }
}

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! Your code must be more generic. You are attempting to hard-code the values. Imagine for a moment that we want to call this function to compare 2 numbers that the user sends us. Your code would only compare 20 and 10. So you'd have to rewrite this function for every imaginable number combination. That's a lot of code and entirely unnecessary. Take a look:

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

Now, when we later call/invoke/execute this function we can determine what numbers to send in. If I were to now call the function like this max(100, 50). It would send in 100 to num1 and 50 to num2. This way we can call the same function over and over and have it compare any number combination we like and get back the max number of whichever two numbers we sent.

Hope this helps! :sparkles:

Ayoade Onafowora
Ayoade Onafowora
2,762 Points

Hi Jennifer Nordell thank you very much. This helped greatly.