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

Michael Zito
PLUS
Michael Zito
Courses Plus Student 4,073 Points

Certainly not correct, but am I close?

I feel like I'm getting there, but need a walk through.

script.js
function max (7, 11) {
  return 7, 11;
if {
 11 >= 7;
}
else {
 7 >= 11; 
}
}
Jamie Carter
seal-mask
.a{fill-rule:evenodd;}techdegree
Jamie Carter
Front End Web Development Techdegree Student 12,096 Points

not far off.

Firstly your function arguments are slightly off. Currently you are passing 7 and 11 in as arguments. But to be more universal you should name these.

function max(numberOne, numberTwo){
  // i can now use numberOne and numberTwo inside the function
}

an if statement always needs a condition, then an action

if (condition){
  // action here
} else {
  // if condition false then do this
}

else does not need a condition, it will occur if the condition is false.

My final answer would be:

function max(one, two){
  if (one > two){
    return one
  } else {
    return two
  }
}

To run said function and pass in real numbers:

max(10,100)

1 Answer

One nudge I'd give is to name the arguments in your function declaration; don't put actiual number values you intend to use with the function yet.

For example:

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

There are other enhancements needed, of course. something to consider; can you return comma separated values? Also, what will happen to the code I declare under my return statement? will it ever run?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return