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
Matt Jones
1,950 PointsJavaScript Operation, need some help.
I have an issue, I've created a function that prints the highest of two numbers and logs it to the console.
I need to add a way of logging the string "Both numbers are the same" if the numbers are equal to one another.
Would this be a case of adding an if statement into the function?
2 Answers
Ted Dunn
43,783 PointsYes, an if/else statement would be a proper way to resolve this issue.
function foo(a, b) {
if a > b
console.log(a);
else if b > a
console.log(b);
else
console.log("Both numbers are the same");
}
Matt Jones
1,950 Pointsfunction bigNum (a, b) {
return Math.max(a, b);
}
var result1 = bigNum (4, 10)
var result 2 = bigNum (20, 20)
console.log(result1);
console.log(result2);
I need result 2 to print "Both numbers are equal" adding an if statement to the funtion with a === b didn't seem to work.