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 trialSaira Bottemuller
Courses Plus Student 1,749 PointsTrouble with Challenge: Creating Reusable Code with Functions
Hi everyone :)
I am having a ridiculous amount of trouble with this Challenge, and I would love some guidance as to where I'm going wrong. Here is the URL for the Challenge if you can't already see it: https://teamtreehouse.com/library/javascript-basics-2/creating-reusable-code-with-functions/create-a-max-function
The instructions are: "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 two numbers and see which is larger, to make it work."
I have a basic grasp (I think?!) on function vs parameter vs argument, but I still don't think I'm going about this the right way. When I looked at what Dave did for his 5 Question Quiz Challenge, his was SO much simpler and more concise than what I tried. I think I'm "thinking" too much here. This is what I'm trying that's not working, please feel free to critique, improve, or scrap it and tell me what I SHOULD be doing instead. As always, thanks for your help, you guys rock!
***It may help to note that I am getting a "Parse Error" when I enter this into the challenge workbox, and when I entered it into Notepad++ and viewed it in Chrome, the console listed it as a syntax error with an unexpected token { on line 31 (which is
} else if {. I tried using parseInt(numberA); followed by parseInt(numberB); and also tried them together as parseInt(numberA, numberB); but I'm not sure they can even be used that way. I ended up using the
if (isNaN) statement to create (hopefully) a message to the user if they're not entering integers. I had also tried a boolean for this with an And statement
{{{---as in, if ( numberA < numberB && numberA === int && numberB === int ) ---}}}```, but that didn't work either. I'm writing all of this up to "Noob Programmer Error" and I'm kind of throwing spaghetti at the wall right now to see what sticks lol :D
function max(numberA, numberB) {
var largest = 0;
if (isNaN( numberA || numberB )) {
alert("Please enter an integer.");
}
if ( numberA > numberB ) {
largest = numberA;
return largest;
} else if {
( numberA < numberB ) {
largest = numberB;
return largest;
} else {
alert("Please enter two DIFFERENT integers.");
}
}
console.log(max(7, 99));
3 Answers
Pedro Sousa
Python Development Techdegree Student 18,546 PointsHello Saira,
You made a typo in the else if. There's an extra curly bracket....
You have this:
} else if {
( numberA < numberB ) {
But you need this:
} else if
( numberA < numberB ) {
Pedro Sousa
Python Development Techdegree Student 18,546 PointsYes, your notes are correct.
Although, my suggestion would be to have new lines like this
if ( ) {
}
else if ( ) {
}
else {
}
Note that, interpreter wise, your notes and my suggestion are exactly the same. But I personally find it easier to read and find errors such as your last one :)
Saira Bottemuller
Courses Plus Student 1,749 PointsI agree, thank you so much Pedro! :)
Saira Bottemuller
Courses Plus Student 1,749 PointsTHAT makes sense as to why it's telling me I have an extra bracket. It wasn't a typo though, it was a lack of understanding on my part. I went back over my notes on If/Else If/Else. It looks like I have my notes right (?) but I just misplaced the curly brace versus the parentheses then?:
SYNTAX for If/Else If/Else:
if () {
} else if() {
} else {
}