Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Write a function that takes an Int and breaks it into its expanded form, returning each component in an array.
func expandTheNumber(_ number: Int) -> [Int] {
var copyOfNum = number
var zeroPlaces = 1
var newArray: [Int] = []
if number < 10 {
newArray.append(number)
} else {
while copyOfNum >= 1 {
newArray.insert(((copyOfNum % 10) * zeroPlaces), at: 0)
copyOfNum /= 10
zeroPlaces *= 10
}
}
return newArray
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment