Skip to content

Instantly share code, notes, and snippets.

@clc80
Created April 21, 2020 02:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clc80/50b7ee1a30c22469e832fa4c680d40ef to your computer and use it in GitHub Desktop.
Save clc80/50b7ee1a30c22469e832fa4c680d40ef to your computer and use it in GitHub Desktop.
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