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

Nela Kay
Nela Kay
11,855 Points

Return larger number...

Hi... so, first I thought I didn't get the question, because it is quite confusing to me. But after trying out several solutions I just got stuck on the conditional. This is my code right now, but it always claims a syntax error... any ideas?

script.js
function max(3,8) {
  if (parseInt(3) < parseInt(8)) {
    return 8;
  } 
}
blake guyan
blake guyan
8,297 Points

those numbers are already integers... no need to parse them.

if you want to create something that works programmatically you are better off like this

function max (x, y) { if (x < y) { return y; } else { return x; } }

and then calling the function like this

max(3, 8);

so when you call the function you are telling it to compare whatever you call as the arguments rather than it just comparing something you already have numbers for :) i know thats a bit of a jump from what you are looking at, but i hope helps

2 Answers

Ari Misha
Ari Misha
19,323 Points

hiya Daniela! In real world issues, i like your approach. But you're doing way too much stuff in context of the challenge. Your function recieves two arguments and your function does a conditional to check which number is greater than the other and return it. All ya have to is return the greater number using "if else" statement. Here is my code

function max(x, y) {
  if(x > y) {
    return x;
  } else {
    return y;
  }
}
blake guyan
blake guyan
8,297 Points

hahah, how about that for posting the exact same thing moments apart!

Ari Misha
Ari Misha
19,323 Points

Hiya blake guyan ! Haha yeah! I dont care as long as Daniela Kersting and other students gets it. (:

Nela Kay
Nela Kay
11,855 Points

Thank you both so much! I kept on trying and I finished the challenge like 20 minutes after asking the question. But with your answers I get it even more.

Thank you!!!