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

Simon Peter Mulima
Simon Peter Mulima
2,655 Points

My function will not work!

How do I get it to return a number

script.js
function max (string1,string2) {
      if (string1>string2) {
    string1
  }
  else { string2
       }
  return max
  }

max (5,6);

2 Answers

Thomas Fildes
Thomas Fildes
22,687 Points

Hi Simon,

Please see the below code I used to pass this challenge:

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

alert(max(2,10));

As shown above, we are to compare the two arguments using an if statement which you have done but we need to return the larger number so if num1 > num2 we should return num1. Otherwise, num2 is larger so we return num2.

It also asks you to place the function call with your choice of two numbers inside an alert dialog.

Hope this helps! Happy Coding!!!

Hi Simon

function max (string1,string2) {
    if (string1>string2) {
    return string1; //if Returns string1
    } else
    return string2; //else return string2
  }
alert(max(5,10)); //call the function with 2 numbers and display it in an alert dialog.