Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

My code should work! I'm making a conditional statement that shows that variable b will be returned if it's bigger!

Why is this not being accepted? I am calling the function and b is being returned as it is truly bigger. I mean, isn't b being returned in this function?

script.js
function max(a,b){
if(a > b){
  console.log('a is smaller, therefore it will not return')
} else{
  return b
};
max(a,b);

I can see that I did not input any value inside the argument of the function that I am using to call the function.

3 Answers

Hi Fransisco,

You are very close to the solution. You really just need to simplify your answer and work on a syntax issue. For the syntax, you will need to close the else portion with an extra "}" closing bracket. Then, you can eliminate the call for max at the end because you will get a return with function that you have, at least according to the challenge. Your code should look something like this:

function max(a,b){
if(a > b){
  console.log('a is smaller, therefore it will not return');
} else{
  return b;}
};

Cheers!

Kuanyshbek Ospanov
Kuanyshbek Ospanov
17,416 Points

You must return a higher variable. But there nothing you are returning

if (a>b) {
 console.log('a is smaller, therefore it will not return')
} 

Try this code

if (a>b) {
 return a
} else {
 return b
}

Hey Francisco, This has been addressed before, but I will give some more insight into how I would think of it. Your max function returns the largest of two numbers. The way you wrote it, you assumed that b would always be the max value. However, the possibility exists where a is the larger value and for that you need to return a since parameters into max could be for example (5,4) or (4,5) and those would have different results. Hope that helps in your thought process for future problems.