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
Alia Khan
3,733 Pointsconditions and operators in swift
Hi I have and nothing seems to have been done about it. Im on conditions and logical operators i understood everything and actually found it pretty simple however the challenege is extremly confusing and doesn't make sense. please help Jennifer Nordell
For this challenge, we'd like to know in a range of values from 1 to 100, how many numbers are both odd, and a multiple of 7.
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 use the not operator to check for "not even
var results: [Int] = []
for n in 1...100 { // Enter your code below
if n = odd && is! even {print ("results")}
2 Answers
Jennifer Nordell
Treehouse TeacherHi again Alia Khan! I have a feeling that the problem here is that you might have forgotten how to use the modulus (or remainder) operator that was covered in this video at around 2:20.
The modulus(remainder) operator will tell us the remainder after dividing one number by another. And we do this with the % sign. So if we have this 12 % 7 the result will be 5. Because 7 goes into 12 one time with a remainder of 5. So if a number is evenly divisible by another number the resulting remainder will be 0. For example, 12 % 6 will have a result of 0 and yes, 12 is evenly divisible by 6. 6 goes into 12 twice with a remainder of 0.
And again, you have the problem where you're printing your result. You're supposed to be adding the results to the array they set up for you in the beginning. This is specified by the challenge.
So here's my solution:
var results: [Int] = []
for n in 1...100 {
// Enter your code below
if n % 2 != 0 && n % 7 == 0 {
results.append(n)
}
// End code
}
First we set up our for loop to check every number between one and 100. If that number is odd (the remainder is not 0 when we divide by 2) and the number is also evenly divisible by 7 (the remainder is 0 when divided by 7) we will add the number to our results array using the append method.
The end result will be that the results array will look like this: [7, 21, 35, 49, 63, 77, 91 ]
Hope this helps!
Alia Khan
3,733 PointsHi thanks for your response. I agree with you how do you think I could work on this? I do find it difficult to apply some of the things I learnt previously. Would you recommed not moving on until I have understand a topic compltely. When I watch the videos I understand whats being said however I struggle on some of the challeges and then still move but go back to it a few days later Is that reccomemded?
Jennifer Nordell
Treehouse TeacherI think any kind of refresher would be great! And sometimes when I need one I do that. For instance, I was learning Git. And I did the workshops here. But then, I got sidetracked with something and put it on hold. So even though I'd finished the workshop, it didn't really matter. Because when I actually got ready to do something with it, I'd forgotten a good 25% of it.
You know what I think I might try? Every 3 or 4 days (assuming you work on this every day), go back and do a challenge you've already done. And pick it sort of at random. Don't do the same challenge twice in a row. I can almost guarantee you that it will get easier and easier. And that might help reinforce what you've learned. Because you did learn it at some point! You even passed the challenge! But what you want is for it to become sort of second nature. Maybe not the syntax (because that you can look up and we've talked about learning things by heart). What you need is a general idea of what it does.
This may be a strange analogy, but I'm going to compare it to a car mechanic. Now, I know very little about cars at all. But I guarantee you that if you walk into a workshop and talk to a guy that's been working there for 2 weeks, the first thing he's going to do is turn around and ask the guy that's been working there for 10 years (documentation). And it's not because that guy is any smarter, it's just because they have the experience. They know where to look for the most likely problem given a particular symptom (debugging). They know which tools they need to get the job done (methods/functions). And they know which parts need to be upgraded/replaced/repaired (objects/classes/data types).
I hope some of this makes sense and is in some way helpful!
Alia Khan
3,733 PointsAlia Khan
3,733 PointsIm so sorry but I dont understand this at all. No where in the video were we shown any remainder operator alongside the logical operators so I would never have thought of that. even un the apple documentation it was pretty simple I dont understand why we need to use the remainder sign at all as its asking about odd numbers and a multiple of 7.
i honestly thing there is a massive gap between the videos and the challenges
Jennifer Nordell
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherHi! Well again, the remainder operator was shown a few videos back in a completely different section. In this video at 2:20.
Part of the art of learning to program is to learn to use the different pieces together and solve a problem. So what they're really asking of you is to combine your pooled learning thus far to solve this problem.
Let's put it this way. In most cases, it would be unrealistic to put a 7 year old in an algebra class. They're still learning to add, subtract, multiply and divide. And they haven't even gotten that down yet. But once they do and they get up to algebra we expect them to still know how to do those basic things to solve more complex problems.
And at some point you knew how to do this. In this challenge you were required to use the remainder(modulus) operator to determine if a number was a perfect multiple. Twenty-one is a perfect multiple of both 7 and 3. Because 21 is both evenly divisible by both 7 and 3. Dividing by either gives a remainder of 0.
My best guess is that this "massive gap" between the videos and the challenges is more because you're having trouble fitting the pieces together. But we're here to help!