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 trialguipriaulx
551 PointsHow is this incorrect?
Question: To start us off, I've written a for loop to iterate over the desired range of values and named the local constant n. Your job is to write an if statement inside the for loop to carry out the desired checks.
If the number is indeed both an odd number and a multiple of 7, append the value to the results array provided.
Hint: To check for an odd number you can either use the remainder operator and 3, or use the not operator to check for "not even"
Preview Output: swift_lint.swift:7:21: error: binary operator '&&' cannot be applied to operands of type 'Bool' and 'Int' if (n % 2 != 0) && (n % 7) {
What is the expected solution?
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if (n % 7 == 0) && (n % 2 != 0) {
results += [n]
}
// End code
}
3 Answers
Steven Deutsch
21,046 PointsHey G P,
I think that this challenge wants you to use the append() method to add items to the array instead of using the compound assignment operator as you are now.
var results: [Int] = []
for n in 1...10 {
// Enter your code below
if (n % 7 == 0) && (n % 2 != 0) {
results.append(n)
}
// End code
}
Good Luck!
guipriaulx
551 PointsAs much as I appreciate your reply, I have already stated that this works! Do you have any supporting evidence to suggest why this is the case? The structure of the course lends itself to using the compound assignment instead, and furthermore, the compile issue output doesn't even reflect the code I had written. There is obviously a bug or greater issue here.
As someone with a computer science background and degree, I have found the technical inaccuracies in the course material to be pretty bad at times, but this is really poor form.
Steven Deutsch
21,046 PointsIt's valid code its just not what the question is asking for in the directions. It's asking you to append, not add the value using compound assignment. Therefore, to successfully answer this question you need to use the method they require.
guipriaulx
551 PointsI can appreciate that. However, at this point in the course, the presenter has asserted that 'I will not ask for the use of a method without writing how to use the method into the question', and both operations achieve the same means. This also avoids the obvious bug in the 'compiler', if you would even call it that...
guipriaulx
551 Pointsguipriaulx
551 Points...turns out it accepts it if I use '.append(n)'.
This seems ironic given that earlier in the series, the presenter was all 'Don't worry about remembering the method names, as we will cover this later in more detail...'. I would expect the non-method solution to be preferred since methods have not formally been covered yet, and the web test environment here doesn't offer intellisense.