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 trialFinn MacLean
2,945 PointsSyntax Error on my values within a function Javascipt
Hello, cant seem to get past this question which i am sure is a basic syntax error.
however:
When i assign values into my parameter, a syntax error is reported.
I know the remainder of the code block is not complete, however when i remove the values, it then reports i have not added the values in to the parameter?
appreciate any help possible, cheers.
function max( 10, 5 ) {
return max();
}
4 Answers
Alex Heil
53,547 Pointshey Finn, you're currently starting the function with real values, to make it re-usable try to go the route with variables. I just checked this scenario and the checker seems to be happy with it, like this:
function max(num1, num2) {
if (num1 > num2){
return num1
} else {
return num2
}
}
hope that helps and have a nice day ;)
Alex Heil
53,547 Pointshey finn, actually the solution is pretty simple and when you read it you'll say "damn, I know that already" :D
now you're using variables (placeholders) in the max-function. so when you call that function you need to give them something to deal with, otherwise the function has nothing to do. therefore you need to pass numbers when you call it, like this:
alert(max(2,3));
the numbers 2 and 3 can be anything you want. these are simply the values the variables num1 and num2 take to work with. hope that helps and have a nice day ;)
Finn MacLean
2,945 PointsYeap, Spot on for knowing my reaction. :-) Guilty of looking at it for too long and over complicating it. ha.
Thanks for all your help Alex, makes perfect sense now.
enjoy the rest of your weekend.
Alex Heil
53,547 Pointsyou're welcome finn, great to hear this helped you out ;)
Erik McClintock
45,783 PointsFinn,
The problem here is actually how you're passing values into the function to stand in as arguments. You are currently assigning values in the parenthesis, as you would if you were CALLING the function; we don't want to do that. We need to give the function arguments so that the function could be reused elsewhere in our code.
The instructions state "Create a function named max() which accepts two numbers as arguments." Here, you need to pass in two variable names as arguments for your function; you're not intended to be passing actual numbers. Recall that functions are built to be reusable pieces of code, so you don't want to assign the values of 10 and 5 yet, as you have done, or this function is wasted (and will not pass the challenge). Think about what the goal of the function is, and then create some appropriately named variables to stand in as arguments. Since this function's job is going to be to compare two numbers, let's just call the arguments "num1" and "num2" for brevity and simplicity.
function max(num1, num2) {
}
Make sure you create your conditional statement inside the function, too, before attempting to pass the challenge.
Hope this helps to make it a bit more clear!
Erik
Finn MacLean
2,945 PointsThanks for your help Eirk,
Having that detailed explanation really helped with the question but also understanding 'Functions' more.
appreciated, cheers.
Finn MacLean
2,945 PointsThanks so much both,
Not using an actually value was what was confusing me and can now understand after your explanations why the arguments must be a variable.
In the second part of the objective it then asks me to call the function within a dialogue box:
function max(num1, num2) { if (num1 > num2){ return num1 } else { return num2 } } alert( max() );
This posts an error saying "Did you pass 2 numbers to the max()
function."
Does this mean i need to use a method to change the variables into values or something simpler than that?
Appreciate both your help this far, thanks again. This question has really puzzled me for some reason. Cheers, Finn