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

Jacob Tennyson
Jacob Tennyson
6,240 Points

What am i doing wrong?

help

script.js
function max(10,20){
  if(10<20){
    return(20);
    }else{
    return(10);
    }

2 Answers

The question wants you to use variables instead of actual numbers as an input. So, you'd start with

function max(num1, num2){

and go from there. Let me know if you need additional help!

Please upvote and mark as best answer if this was helpful!

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

I assume you want your function to take 2 numbers and return the bigger number? If so

//use variables in your function
//instead of numbers
function max(num1, num2){
    if(num1 < num2){
        return(num2);
    }else{
        return(num1);
    }
}

//call the function with numbers
alert( max(10, 20) );

I alerted the results of using the function but you can also just store the bigger number in a variable and use it later

var biggestNum = max(10, 20);