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

Derek Hutson
Derek Hutson
9,740 Points

Making max Function

Hi all,

Currently stuck trying to make max function.. I think I am close but missing a small piece of it somewhere!

script.js
function max(numberOne, numberTwo) {
 var numberOne = Math.random();
 var numberTwo = Math.random();
    if (numberOne > numberTwo) {
    return numberOne;
  } else if (numberOne < numberTwo) {
    return numberTwo;
  }
}

1 Answer

Dehn Hunsworth
Dehn Hunsworth
7,189 Points

think they want you to create the random numbers outside the function then pass them to it.

function max(numberOne, numberTwo) {
    if (numberOne > numberTwo) {
    return numberOne;
  } else  {
    return numberTwo;
  }
}
 var numberOne = Math.random();
 var numberTwo = Math.random();

alert(max(numberOne, numberTwo));