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 trialJaelyn Fields
897 PointsI'm not sure what I did wrong I've been try for so long.
function max(num1,num2) {
return num1 + num2;
}
let num1 = 5;
let num2 = 4;
3 Answers
jb30
44,807 PointsCurrently, your function returns the sum of the two numbers rather than the maximum number. The challenge wants you to return the value for num1
if the value for num1
is greater than the value for num2
and otherwise return the value for num2
. In your code, since num1 = 5
and num2 = 4
, your function should return its value for num1
, which is 5
. If instead you had set num1 = 5
and num2 = 6
, your function should return its value for num2
, which would be 6
.
Rabin Gharti Magar
Front End Web Development Techdegree Graduate 20,928 PointsHey Jaelyn Fields,
The simple way to do this is by using a if else statement
, this way it'll return the larger number out of two. The code will look like this:
function max(num1, num2) {
if(num1 > num2) {
return num1;
} else {
return num2;
}
}
Kristjan Vingel
7,992 PointsEssentially you have to implement the Math.max() function.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max
Jaelyn Fields
897 PointsThank you I appreciate it!!
Jaelyn Fields
897 PointsJaelyn Fields
897 PointsThanks I didn't realize what it meant!