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 trialronanmcguire
4,497 PointsWhy am I being told I did not create a function?
I am being told I did not create a function here.
function max( a,b ) {
var firstNumber = a;
var secondNumber = b;
if (firstNumber.value > secondNumber.value) {
return true;
} else {
return false;
}
}
max(1,2);
4 Answers
Stephen Van Delinder
21,457 PointsRonan,
The problem is in your use of the .value property. I usually see the .value property used to select specific form data or options. In some cases when you compare numbers, you may want to ensure that you are comparing two integers rather than strings, but you would do that with the parseInt() function, not the value property.
In this case, we can assume that the inputs will be integers and just compare the variables directly.
You could just remove the value property and your code would function just fine:
function max( a,b ) {
var firstNumber = a;
var secondNumber = b;
if (firstNumber > secondNumber) {
return true;
} else {
return false;
}
}
max(2,1);
But this code returns a boolean true or false. How can you update it to always return the higher value?
Hope this helps!
Stephen Van Delinder
21,457 PointsThat works perfectly! Using well defined variables is great when developing on a team and debugging code, but you could also refactor the code above as follows:
function max( a,b ) {
if (a > b) {
return a;
} else {
return b;
}
}
max(2,1);
Hope this helps. Keep going, JS is a very valuable skill to have.
Good luck Ronan!
ronanmcguire
4,497 Pointsah I see! so would simply inserting the variable work?
function max( a,b ) {
var firstNumber = a;
var secondNumber = b;
if (firstNumber > secondNumber) {
return firstNumber;
} else {
return secondNumber;
}
}
max(2,1);
ronanmcguire
4,497 PointsThanks!