For example say I have the array:
let nums = [1, 2, 3, 4, 5, 6]
I would like to output a new array with the cube values: [1, 8, 27, 64, 125, 216]
Do I have to use a loop?
For example say I have the array:
let nums = [1, 2, 3, 4, 5, 6]
I would like to output a new array with the cube values: [1, 8, 27, 64, 125, 216]
Do I have to use a loop?
You can use map() and pow() together:
import Foundation
let nums = [1, 2, 3, 4, 5, 6]
let cubes = nums.map { Int(pow(Double($0), 3)) }
let raisedBySix = nums.map { Int(pow(Double($0), 6)) }
print(cubes) // [1, 8, 27, 64, 125, 216]
print(raisedBySix) // [1, 64, 729, 4096, 15625, 46656]