Guest User

Untitled

a guest
Dec 10th, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. /* 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.
  2.  
  3. Example
  4.  
  5. sumOfDigits(23) // returns 5
  6. sumOfDigits(496) // returns 19
  7.  
  8. Thoughts: This does not work with one digit because I don't have anything to add it to
  9. example:
  10. sumofDigits(5) // Error
  11.  
  12. 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
  13.  
  14. I know we can do something like thsi but that passes in a string not an int.
  15.  
  16. let number = "1256"
  17.  
  18. let array = number.characters.flatMap{Int(String($0))} /
  19.  
  20. */
  21.  
  22.  
  23. func sumOfDigits(number: Int) -> Int {
  24.  
  25. return int
  26. }
Add Comment
Please, Sign In to add comment