Created
April 21, 2020 02:27
-
-
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.
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