Write a function that takes an Int and breaks it into its expanded form, returning each component in an array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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