Sobes.tech
Intern — Middle

What is the output? – option 28

livecode

Task condition

In this example, we create a CelestialBody structure containing a name and a distance from the Sun, form an array of objects, and then apply two standard collection algorithms: map to get a list of names and reduce to calculate the total sum of distances.

struct CelestialBody {
    let designation: String
    let orbitalRadius: Double
}

let bodies = [
    CelestialBody(designation: "Mercury", orbitalRadius: 0.387),
    CelestialBody(designation: "Venus", orbitalRadius: 0.722),
    CelestialBody(designation: "Earth", orbitalRadius: 1.0),
    CelestialBody(designation: "Mars", orbitalRadius: 1.52),
    CelestialBody(designation: "Jupiter", orbitalRadius: 5.20),
    CelestialBody(designation: "Saturn", orbitalRadius: 9.58),
    CelestialBody(designation: "Uranus", orbitalRadius: 19.2),
    CelestialBody(designation: "Neptune", orbitalRadius: 30.1)
]

let namesList = bodies.map { $0.designation }
let totalDistance = bodies.reduce(0) { $0 + $1.orbitalRadius }