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

Sandy Hirst
Sandy Hirst
1,294 Points

Code works in workspace but not in assignment.

This code works in my workspace and runs fine, but it keeps giving me a "wrong" answer in the assignment. Any advice?

script.js
function max(a, b) {
    if (a > b) {
    return(a + " is the bigger number!");
  }

  else {
    return(b + " is the bigger number!");
   }
}
console.log(max(10, 20));

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points

At least in an real environment this would not work since in the else statement b can be bigger or equal to a.

Also the challenge only wants you to return the number and not a sentence (correct if I’m wrong, that’s my impression)

Last, you don’t need the log statement and it could break the challenge. The challenge is calling the function with its own set of parameters

Sandy Hirst
Sandy Hirst
1,294 Points

Yes. You are right. I took out the sentence and only returned the number and it worked.

Thanks for your help!