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
Jimel Atkins
2,093 PointsJavascript Functions
Create a new function named max which accepts two numbers as arguments.
The function should return the larger of the two numbers HINT: will need a conditional statement to test 2 parameters to see which is larger.
So i have written
function max (num5, num12) {
var answer =("num5, num12")
if (5 < 12) {
}
return answer }
2 Answers
Ken jones
5,394 PointsYou can do this a few ways.
You can use an if else statement like so...
function highestNumber(a, b) {
if (a > b) {
return a
} else {
return b
}
}
var result = highestNumber(100, 46); // will return 100
OR you could use javascripts Math.max method. This is a lot cleaner in my humble opinion....
function highestNumber(a, b) {
return Math.max(a, b);
}
var result = highestNumber(40, 18); // will return 40
Jesus Mendoza
23,289 PointsHey Jimel,
You have a few mistakes in your code
- You are setting the variable answer value to ("num5, num12").
- You are comparing 5 < 12, it should be something like num5 < num12.
- You are returning answer which has the value ("num5, num12") on it.
// Create a function that accepts 2 parameters
function max(num1, num2) {
// If num1 is greater than num2 it means that num1 is the largest of the two numbers, so we return num1.
if (num1 > num2) {
return num1;
// If num1 is not greater than num2 it means that num2 is the largest of the two, so we return num2.
} else {
return num2;
}
}
// Now we call our function with the two numbers we want to compare.
max(5, 12);