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 Functions Pass Information Into Functions Create a max() Function

Aaron McGlothin
seal-mask
.a{fill-rule:evenodd;}techdegree
Aaron McGlothin
Full Stack JavaScript Techdegree Student 1,764 Points

Im so stuck its so confusing :(

script.js
function max(20,10){
  {
function max(15,25){
}
function max(32, 70) {
    if (1 > 2) {
        return a;
    }
{
  function max(a, b) {
    if (a > b) {
        return a;
    } else {
      return b;
    }
}    
max(20,10) // => 20, CORRECT
max(10,20) // => 20, CORRECT
max(0,100) // => 100, CORRECT
max(45,-45) // => 45, CORRECT  

2 Answers

Steven Parker
Steven Parker
231,269 Points

In task 1, you create the "max" function. There should only be one function created with this name. The last one you have above is good, but the others before it are causing syntax errors.

Then in task 2, the instructions say to call the max function with two number arguments. You only need to do this one time (not 4 as above). But they also say: "*Display the result of the function in the browser's JavaScript console.", so you need to use the console.log function to show what calling the max function returns.

Charlie Palmer
Charlie Palmer
15,445 Points

I'm not too sure what you mean, but when i was first learning to program on here I was also struggling with learning very basic things like functions since i had never done it before and i was mis understanding functions. I am going to assume that you have just started to learn functions so i'm going to do my best to explain them to you.

Functions are very useful in programming and basically everything will be done using a function of some sort. in order to create a function you will need to use the key word 'function'. and then the next word will be the name of the function. so in this case you want to create a function named function. this would give you

function max

This however doesn't work. After the name of the function you need to use these () in and then two curly braces which are these {} So now you should have something which looks like this

function max() {}

You now have a function called max, however this function doesn't do anything yet. Anything code you want this function to run. For example i want this function to print "Hello World" when called so i'm going to add the functionality to the function by adding the code inside of the curly braces.

function max() {
  console.log("Hello World");
}

this function will now print "Hello World" into the console when the function is called. How do you call the function? in order to do this you will need to write the name of the function followed by the brackets:

max()

Now let me explain the parameters to you. Lets say had a function which was called printMyStatement and it took two parameters and i wanted it to print the two variables which i passed in. The function would look like this:

function printMyStatement(a, b) {
  console.log(a);
  console.log(b);
}

As you can see this function takes in two paramers but how do you use them? lets call the function and see what the output is going to be:

printMyStatement("Hello", "World");

Output:

//Hello
//World

Hopefully that explains how functions work.

For the task which you have been set you want to use the following code:

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

console.log(max(404, 200));

Please Don't just paste the answer from here, try and actually break down what I have said and to understand it.