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
senay yakut
4,124 Pointsjavascript
Create a new function named max which accepts two numbers as arguments(you can name the arguments, whatever you would like).The function should return the larger of the two numbers.
HINT:You'll need to use a conditional statement to test the 2 parameters to see which is the larger of the two.
olusoji ojerinde
2,048 Pointsfunction max(temp1, temp2) { var numbers= 60 > 30; if (max === temp2) { return true; } else { return false; }
4 Answers
senay yakut
4,124 Pointshello Kevin, I am a beginner coder , sometimes I am doing very basic mistakes, thanks for your help
Kevin Korte
28,149 PointsYou're kinda there olusoji.
You have your basic function structure correct
function max(temp1, temp2) { }
var numbers wouldn't be correct, so lets cover that. We know we're passing in two temps, so what we can really do is see which one is bigger than the other and than return that number - not return true or false like what you have. So if we continue on, we have
function max(temp1, temp2) {
if (temp1 > temp2) {
return temp1;
} else {
return temp2
}
}
max(60,30);
Great, so now we check if temp1 is greater than temp2, we return temp1, if not we assume temp2 is greater and return that!
This almost works, except for, what happens if we pass in max(30,30), both are the same, one is not greater than the other, so be better refactor to cover that.
function max(temp1, temp2) {
if (temp1 > temp2) {
return temp1;
} else if (temp1 < temp2 {
return temp2
} else {
return "They're both the same!"
}
max(60,30);
Great, now we have something usable, if temp1 is greater than the other, it's returned, if temp 2 is greater, it's returned, and if neither temp1 or temp 2 are greater, we can safely assume they're the same, and return a message back that says so.
Now, this code may not pass a quiz exam, as they're very specific, but it would work in the wild.
olusoji ojerinde
2,048 PointsThanks kelvin, the first code worked.
Kevin Korte
28,149 PointsYes I agree with Jennifer, please provide us with the code that you tried and has failed, so we can help you understand why it's wrong. I try not to every just provide answers to quizzes.
olusoji ojerinde
2,048 Pointshi jenifer & kelvin, i tried solving this but not getting through. Can someone help with the right code:
function max(temp1, temp2) { var numbers= 60 > 30; if (max === temp2) { return true; } else { return false; }
Kevin Korte
28,149 Pointswhat's the link to the challenge?
var numbers = 60 > 30 doesn't make sense, isn't needed, and isn't used.
It's wanting you to return the higher number, you're returning true or false. And your conditional statement isn't testig if one is bigger, it's just testing if they're exactly the same.
Read back through my post here, the answer is right there.
senay yakut
4,124 PointsBeneath the max function you just created, call it with two numbers and display the results in an alert dialog. Pass the result of the function to the alert method.
For example, to display the results of the Math.random() method in an alert dialog you could type this:
here is another one
Kevin Korte
28,149 Pointsis this a quiz, what does your attempt look like? I don't want to just give you the answers.
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi there, Senay! First, it would be helpful for others to answer if you could link the challenge. However, in this instance I could give you the correct code, but that would sort of defeat the purpose. The challenge is there to test your mastery of the subject. I would prefer to see at least a "good faith" effort on your part by showing us what code you've tried that's failing. This would be an overall better learning experience for you, in my opinion. That way we can say specifically what it is that you're not understanding or writing incorrectly. Good luck!