Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* a function called sumOfDigits that given an integer, returns the sum of its digits. If you need an extra challenge, find a solution that works without using Strings.
- Example
- sumOfDigits(23) // returns 5
- sumOfDigits(496) // returns 19
- Thoughts: This does not work with one digit because I don't have anything to add it to
- example:
- sumofDigits(5) // Error
- Process: I need to pass an integer through and the expand it into single digits and then add the digits together and return the sum
- I know we can do something like thsi but that passes in a string not an int.
- let number = "1256"
- let array = number.characters.flatMap{Int(String($0))} /
- */
- func sumOfDigits(number: Int) -> Int {
- return int
- }
Add Comment
Please, Sign In to add comment