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

Omar Juárez
Omar Juárez
2,737 Points

i don´t understand what i have to do

i don´t understand what i have to do

script.js
function max(uno,dos){

}

2 Answers

The Question: 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.

when we look at this questions we see several things need for us to complete the challenge:

  1. a function named max

  2. two parameters (not arguments)

  3. the function needs to return either parameter.

we also have a hint to use an if statement.

Lets go in order of the question:

step 1. make a function named max, which you have

function max() {

}

step two. two parameters:

function max(a,b) {

} 

you have uno and dos, which is also fine.

step 3. return the larger parameter:

in order to do this we need to use the hint. an if statement can choose which is grater than or less than:

function max(a,b) {
      if (a > b){
       return a //here we are saying if a is greater than b return the value a  
      } else {
       return b //here we are saying if the statement of if a > b than return the value of b 
       }
} 

Sometimes the challenges can be tricky because they are very strict because they are programs that evaluate your code. Remember to try and go through the questions and find what they are wanting and go step by step that way becoem a better programmer and get threw the challenges.

I hope this helps.

Omar Juárez
Omar Juárez
2,737 Points

Thank you it was useful, thank you so much