Created
October 2, 2019 15:37
-
-
Save bradleyyin/f9ec902c84c78d71aec1fbf75c4a9826 to your computer and use it in GitHub Desktop.
morning code challenge week 13
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
import Foundation | |
func makeChangeAsString(fromAmount: Double, withCost: Double) -> String { | |
guard fromAmount >= withCost else { return "Invalid change"} | |
let change = fromAmount - withCost | |
var changeInCent = Int(change * 100) | |
let dollar = changeInCent / 100 | |
changeInCent -= dollar * 100 | |
let quarter = changeInCent / 25 | |
changeInCent -= quarter * 25 | |
let dime = changeInCent / 10 | |
changeInCent -= dime * 10 | |
let nickel = changeInCent / 5 | |
changeInCent -= nickel * 5 | |
let penny = changeInCent | |
return "Your change is $\(change). That is \(dollar) dollars, \(quarter) quarters, \(dime) dime, \(nickel) nickels and \(penny) pennies." | |
} | |
makeChangeAsString(fromAmount: 5.00, withCost: 2.15) // returns "Your change is $2.85. That is 2 dollars, 3 quarters, 1 dime, 0 nickels and 0 pennies." | |
makeChangeAsString(fromAmount: 10.00, withCost: 2.38) // returns "Your change is $7.62. That is 7 dollars, 2 quarters, 1 dime, 0 nickels and 2 pennies." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment