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

Guadalupe Favela
Guadalupe Favela
5,564 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

Juan Lopez
Juan Lopez
Full Stack JavaScript Techdegree Student 20,028 Points

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
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);

///