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

Jaime Rios
PLUS
Jaime Rios
Courses Plus Student 21,100 Points

What's wrong with this code?

Please check the code, I'm at the Create a max() function on JavaScript Basic

script.js
function max(youA, youB) {

if (youA>youB) {
  alert(youA  " is larger than " youB );
}
else {
alert(youA  " is larger than " youB );
}
}

1 Answer

Sam Roberton
Sam Roberton
7,225 Points

You need to add concatenation (+)

function max(A, B) {

   if (A > B) {
      alert(A + " is larger than " + B);
   } else {
      alert(B+ " is larger than " + A);
   }

}

Hope this helps :)

Jaime Rios
Jaime Rios
Courses Plus Student 21,100 Points

Yeah, it was also missing a return value. But thanks :D

Now it asks me to pass two numbers to the function,

function max(youA, youB) {

if (youA>youB) { alert(youA + " is larger than " + youB ); return youA; } else { alert(youA + " is larger than " + youB ); return youB } }

alert( max(4, 34) );