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

Dolly Kallab
Dolly Kallab
925 Points

how can i define a function?

how can i add numbers to max to be able to compare ?

script.js
function max(num1, num2) {
 if (num1 > num2){
  return num1 +" is the larger number";
 } else{
   return num2 + " is the larger number";

    }

max(1,2);

}

8 Answers

Erik Nuber
Erik Nuber
20,629 Points
function max(num1, num2) { 
var max; if (num1 > num2){
 max = (num1 + " is the larger number");
 return(max); 
} else{ 
max = (num2 + " is the larger number"); 
return(max);
}
}

max(1, 2);

Ive tried running it in the console and, it works fine, it spits out 2 is the larger number as expected. What is it you are trying to do? Because it does not actually return a number, it returns a string.

if you want just a number but also want the string you could do

var x = max(1, 2);
var y = parseInt(x);

In this case x would be a string stating "2 is the larger number" and y would just equal 2 which is a number.

Mike Nunyabeez
Mike Nunyabeez
4,677 Points

If you want to do more comparisons within the function, than you would want to write an else if statement....

if (x > 5) {

} else if (x > 50) {

} else {

}
Erik Nuber
Erik Nuber
20,629 Points

There you go... sorry again.

Mike Nunyabeez
Mike Nunyabeez
4,677 Points

In response to Dolly. ... Here is an example of proper format.

function max(num1, num2) {
  if (num1 > num2) {
    console.log(num1 + " is the larger number");
  } else {
    console.log(num2 + " is the larger number");
  }
}

max(1, 2);
Erik Nuber
Erik Nuber
20,629 Points

The quizzes are very specific. You are asked to put the larger number into an alert...as so...

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

alert(max(1, 2));
Dolly Kallab
Dolly Kallab
925 Points

Thank you Erik that was the answer... and you thank you for the great feed back, i did learn a lot ..

Erik Nuber
Erik Nuber
20,629 Points

You should take the max(1, 2) outside the function and you can list them then there.

function compareNums(num1, num2) {
var greaterNum;
 if (num1 > num2){
  greaterNum = (num1 + " is the larger number");
  return(greaterNum);
 } else{
   greaterNum = (num2 + " is the larger number")
   return(greaterNum);

    }
}

var x = compareNums(1,2);
document.write(x);
console.log(compareNums(45, 12));
document.write(compareNums(24, 52));

etc.

Dolly Kallab
Dolly Kallab
925 Points

Thank you Eric for your quick answer, but it didn't work

Erik Nuber
Erik Nuber
20,629 Points

my fault, I didn't fix your code, I will in just a second. I was just answering your actual question and hadn't paid attention to the rest of it. I'm sorry...

Dolly Kallab
Dolly Kallab
925 Points

still not working it says that function max doesn't return a number, I've updated my code to the following: function max(num1, num2) { var max; if (num1 > num2){ max = (num1 + " is the larger number"); return(max); } else{ max = (num2 + " is the larger number"); return(max);

}

}

max(1,2);

Mike Nunyabeez
Mike Nunyabeez
4,677 Points

You have a syntax error. Your final curly brace should be before you call the function and pass the arguments. If you want to try multiple sets, just call the function multiple times and pass a different set each time. The question is a little unclear to me, but this is what I think you are looking for.

Mike Nunyabeez
Mike Nunyabeez
4,677 Points

Indentation is key to writing good scripts. Thats why I tell everyone to get OUT OF WORKSPACES and start writing code in Atom with LINTER!! :)

Dolly Kallab
Dolly Kallab
925 Points

thank you Mike, you are right :)

Dolly Kallab
Dolly Kallab
925 Points

and still not letting me pass the the quiz :(