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

Lu Favela
seal-mask
.a{fill-rule:evenodd;}techdegree
Lu Favela
Front End Web Development Techdegree Student 15,571 Points

function should return the larger of the two numbers.

I am having trouble understanding "parameters" and its asking me to return the larger number using conditional statement

script.js
function max(num1,num2){
return num2
}

if(num1<num2){
   return num1;
   }else{
   return num2;
   }

2 Answers

First off, great job so far on what you have written down. Just remember for the conditional if statement to work with the num1 and num2 parameters you'll have to move that if statement inside the max function. Also in the directions it is asking for you to compare the 2 numbers and return the larger of the 2. Currently in your if statement it is returning the smaller.

Lucas Guimarães
seal-mask
.a{fill-rule:evenodd;}techdegree
Lucas Guimarães
Front End Web Development Techdegree Student 3,900 Points

I think I have another option as well to solve this challenge.

function max(num1, num2) {

  if ( parseFloat(num1) < parseFloat(num2) ) {
   return num2;
   } else if ( parseFloat(num1) > parseFloat(num2) ) {
   return num1;
   } else {
    document.write(num1 + " and " + num2 + " are equal!");
}
}

/* That is just an exemple and pratice for 
  commenting */

document.write(max(1, 5) + "<br>");
document.write(max(8.89, 5.52) + "<br>");
max(5, 5);

///