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 trialMaxence Roy
8,753 PointsI'M trying this and it's not working, I can't figure out the solution..
for n in 1...100 {
// Enter your code below
if n != even {
results += results[n]
}
// End code
}
[MOD: edited code block - srh]
1 Answer
Steve Hunter
57,712 PointsHi Max,
You need to test if each number between 1 and 100 is both odd and a multiple of 7. Unfortunately, there's no method called even
that returns a true/false
response - that would be helpful! You test for 'oddness' by dividing by 2 and seeing if there's a remainder. If there is, the number was odd, else it was even. So, if n % 2 != 0
gives you a test of oddness, or a lack of evenness, whichever you prefer.
You'll then need to chain that (use &&
) with a test of n % 7 == 0
. If both pass, append n
to the results
array with results.append(n)
.
I hope that helps - let me know how you get on.
Steve.