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

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would l

I'm failing to produce the answer they want here. Please assist, thanks.

script.js
function max(upper, lower) {

    if (upper>lower) {
      return upper;
      } 
}

2 Answers

yk7
seal-mask
.a{fill-rule:evenodd;}techdegree
yk7
Full Stack JavaScript Techdegree Student 22,891 Points

Hello :) your code is working locally, on my pc, but for the best practice: you need to add the else statement. I tested with your code and added the "else{...}" and it worked.

First I think its good idea to make sure that both of them are numbers, then if upper is not greater than lower return lower:

function max(upper, lower) {
    // make sure that upper and lower are numbers
    if (isNaN(upper) || isNaN(lower)) {
        console.log('upper and lower should be numbers')
        return;
    }

    if (upper>lower) {
        return upper;
    } 
    return lower;
}