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

Another JavaScript Challenge Issue

I can't figure out the code challenge to this. Create a new function named max() which accepts two numbers as arguments. The function should return the larger of the two numbers. You'll need to use a conditional statement to test the 2 numbers to see which is the larger of the two.

5 Answers

Based on your description of the challenge, you'll want to create a function that can take two arguments and then use if-else structures to evaluate which number is bigger and return that number.

function max(numA, numB) {  // The function needs to accept two numbers.
  if (numA > numB) {  // If numA is greater...
    return numA;  
  } else if (numB > numA) {  // Otherwise if numB is greater...
    return numB; 
  }
}

Edit - Whoops! Sorry about that, there was a typo in my code. Fixed.

Unfortunately I am getting this when i put it this answer, which is what close to what i had before i asked for help. Bummer! There was an error with your code: SyntaxError: Parse error

Sorry about that! There was a typo in my code. I've fixed it and tested it on the challenge this time. You should be good to go!

Thank you!!!!!

Still doesnt work for me :S

I was also having trouble with this challenge. Thanks!

You are missing an equal case and you can do in a simpler way.

The ? operator checks the previous expression (a >b) and the first value is returned if true, otherwise the second value.

function max(a, b) {
    if (a == b) {
        return 'equal';
    }
    return a > b? a : b;
}

It works but I don't understand what all this is.
return a > b? a : b;

Inside a function "return" returns a value. a > b is the condition I am checking for

the ? is the ternary (conditional operator) in JavaScript So the syntax for the statement is this: condition ? expr1 : expr2

Where expr1 gets returned if the condition is true and expr2 gets returned if the condition is false.

An equivalent (and probably more readable version is this:

if (a > b){
  return a;
}else{
  return b;
}

I just tried to make it concise as possible. Hope that helps.

Ok my mind is blown is your mind blown but I tried it the simple way and it still dosent work. What is a ternary

Ternary (by definition) means composed of three parts.

So the "?" is called a ternary operator, since it has the following:

conditional ? expr1 : expr2

    function max(a,b){
        if (a == b){
            return 'equal';
        }
        if (a > b){
          return a;
        }else{
          return b;
        }
    }
    alert(max(3,4)); //Returns 4
    alert(max(4,3)); //Returns 4
    alert(max(4,4)); //Returns equal

Don't forget the closing {}. Hope that helps.