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

Matthew Geiter
seal-mask
.a{fill-rule:evenodd;}techdegree
Matthew Geiter
Full Stack JavaScript Techdegree Student 13,739 Points

completely lost any help?

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

script.js
function max(wins, loses) {
  return max;
  var wins = 10;
  var loses = 6;
  return max; 
}
var winner >= 10;
if (wins >= 10 ) {
  alert('winner');
} else {
 alert('loser');  
}

1 Answer

Mark Rinkel
Mark Rinkel
13,501 Points

Hey, you're a ways off here. Here's some comments that might help.

function max(wins, loses) {

  /* max is the name of your function. it's not returning the larger of the two, it's returning the function itself. */
  return max;

  /* Since this function returned, this code is entirely unreachable. It will never execute. */
  var wins = 10;
  var loses = 6;
  return max; 
}

Before the function max(wins, loses) is called we don't know which will be larger (ie. the 'winner'). Let's rename the two parameters to number1 and number2.

function max(number1, number2) {
   /* we need to figure out which number is larger. We can do this with >  or <. */
  if (number1 > number2) {
    // This code will ONLY execute if number1 is bigger than number2.
    return number1;
  }
  else {
     // This code will only run if number1 was NOT bigger than number2.
     return number2;
  }