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

Hi, how to solve this case?

Problem again ;/

script.js

Post some of your code so we can see what your issue is.

1 Answer

Daniel Markham
Daniel Markham
10,976 Points

Hi Dargis,

You want to create a JavaScript function that will accept two numbers (arguments, parameters) and return the larger number. Thus, you need to:

  1. define a new function. E.g.
var max =function(){}
  1. put two arguments within that function E.g.
var max= function(number1placeholder,number2placeholder){}
  1. and last, tell the function what "to do" with your arguments. In this case we want the function to "return" the larger number, i.e. the max. If I am comparing the numbers 1 and 100, my function should return the larger number (100). Thus, you want to use a conditional statement (IF/ELSE) that compares each of the arguments and then RETURNS the larger number.

Below is a script that goes over this.

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


}

alert(max(1,100));