Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Creating Reusable Code with Functions Create a max() Function

Create a new function named max which accepts two numbers and I get msg 'max' function isn't returning a number.

Create a new function named max which accepts two numbers as arguments (you can name the arguments, whatever you would like). The function should return the larger of the two numbers.

HINT: You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.

function max(num1,num2) { var num1 = prompt("Please enter first number"); var num2 = prompt("Please enter second number");

if (num1 > num2) { return num1; } else { return num2; } }

script.js
function max(num1,num2) {
  var num1 = prompt("Please enter first number");
  var num2 = prompt("Please enter second number");

  if (num1 > num2) {
    return num1;
  }
  else {
    return num2;
  }
}

4 Answers

Bryan Knight
Bryan Knight
34,214 Points

Hi Dennis,

I think you were over thinking this one. You have the answer but you need to remove the

  var num1 = prompt("Please enter first number");
  var num2 = prompt("Please enter second number");

What's happening is num1 and num2 are being passed into the function but those two lines immediately overwrites the values of num1 and num2 with the prompt value which is not needed. All you need is

function max(num1, num2){
  if(num1 > num2){
    return num1
  }else{
    return num2
  }
}

Once you learn ternary operator you can do this in one line

function max(num1, num2){
  return num1 > num2 ? num1 : num2;
}

FYI as a side note, prompt returns a string. So best practice would be to parseInt() the returned value of prompt

var num1 = parseInt(prompt("Please enter first number"));
var num2 = parseInt(prompt("Please enter second number"));
Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,704 Points

Hey Dennis,

Your code is pretty much fine, except you are adding two lines that the challenge didn't ask for. If you delete the two var lines you added, you'll be good to go.

function max(num1,num2) {
  if (num1 > num2) {
    return num1;
  }
  else {
    return num2;
  }
}

Keep Coding! :dizzy:

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Dennis,

Quick solution here: the challenge does not required any input, just set up the function as you did but get rid of the inputs and the compiler will take care of the input to test your code.

Vitto

My solution ddidnt use an else.

max(a,b) {
if (a<b){
return b;
}
}