WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Unwrap app. Free Coding task. Will be glad for advice how to solve.

Forums > Swift

Hello everyone!

I'm trying to solve next task:

"Write code that loops from 1 through 100 to create an array of all even numbers"

In first look it's a simple quastion so i've tried next solutions:

1) Simple "for in" loop.


var arrayOfNewEvenNumbers = [Int]()

for number in 1...100 { if number % 2 == 0 { arrayOfNewEvenNumbers.append(number) } }


  1. Simple function with "for in" loop.

func getEvenNumbers() -> [Int] {

var arrayOfEvenNumbers: [Int] = []

for number in 1...100 {
    if number % 2 == 0 {
        arrayOfEvenNumbers.append(number)
    }
}
print(arrayOfEvenNumbers)
return arrayOfEvenNumbers

} getEvenNumbers()


Tried a lot of times in Xcode and all of this variations is working but Unwrap app won't approve it.

Seems like this task is not easy as it looks or i have a misstake in my code.

Will be glad for advices where i should pay more attention or which method i should to use to solve this task.

   

Try using

for number in 1..<101 {
// etc

When type the three dots in the phone it make … as one character not ... as three separate characters

1      

Tnx for amazing advice. It's finally working!

   

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.