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
Jonathan Hamada
4,810 PointsCounting the Tree House Way: Using Functions and Conditionals
How do replace an integer that is divisible 5 by and replace it with the string, "Tree" and if divisible by 10 print house? So it would look like this:
1
2
3
4
Tree
6
7
8
9
Tree House
11
12
13
14
Tree
16
17
18
19
Tree House
Counting, the Tree House Way Print integers 1 to 100. If divisible by 5, print "Tree" instead. If by 10, also print " house".
function integers() {
for (var i = 1; i < 101; i++) {
document.write(i + " </br>");
if (i % 5 === 0 ) {
document.write("Tree" + " </br>");
if (i % 10 === 0 ) {
document.write("House" + " </br>");
}
}
}
}
integers()
3 Answers
nico dev
20,364 PointsYou almost nailed it! :)
Just remember that if provides a lot of options to play with the if/else if/else set.
So if a number is divisible by 5, it will surely be divisible by 10, right? That gives you a chance to include the 10 in an if inside the "5 if" (if we can call it that way).
Now what happens if none of those is true, then you want anything else to print out the i value, right?
If you're doing this for the sake of practice, practice, practice (and I think you're doing so), I encourage you to try to solve it before checking the code below, just reading the clues/hints above.
Otherwise, read below one of the possible ways to solve it:
function integers() {
for (var i = 1; i < 101; i++) {
if ( i % 5 === 0 ) {
document.write("Tree" + " </br>");
if ( i % 10 === 0 ) {
document.write("House" + " </br>");
}
} else {
document.write(i + " </br>");
}
}
}
integers()
Jonathan Hamada
4,810 PointsThank you!
Jonathan Hamada
4,810 PointsI didn't see this message til now. So today I continued to try and solve it with using parameters and swapping values. All which didn't work. Now the correct code, all had to do was add the print the i value at the end using an else condition. Thank you sir for taking the time to show me the correct way. Just curious, in this case I didn't need to use a parameter. When would I be forced to use parameters?
nico dev
20,364 PointsGreat to know it helped you.
About your last question (good one!), not sure to be honest. The way I see it, I guess the only instance when you need to use parameters, is if you will use the corresponding arguments inside the function.
Here I found examples of both.
Although I will be pleased to be corrected if I'm wrong, cause that would also mean learning for me. :)
Jonathan Hamada
4,810 PointsThank you Nico
Steven Parker
243,656 PointsSteven Parker
243,656 PointsPlease provide a link to the course page to allow for a complete and accurate analysis.