UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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

3      

Tnx for amazing advice. It's finally working!

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.