[❗️SWIFT❗️] Rounding issue with (?) Kaleidoscope code coach | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

[❗️SWIFT❗️] Rounding issue with (?) Kaleidoscope code coach

From Code Coach, Kaleidoscopes, SWIFT!!!! let ScopeInput:String! = readLine() var ScopesOrdered:Double! = Double(ScopeInput) var FinalPrice: Double = 0 if ScopesOrdered > 1 { //Apply discount (10 percent) FinalPrice = (ScopesOrdered * 5) * 0.90 //Apply Taxes FinalPrice += (FinalPrice * 7/100) } else { FinalPrice = (ScopesOrdered * 5) //Calculate base price for one scope - without a discount FinalPrice += (FinalPrice * 7/100) //Apply Discount } FinalPrice *= 100 FinalPrice.round() FinalPrice /= 100 print(FinalPrice) So after several tests and revisions, my code passes all but one - 4 tests and one of them is hidden. (The only test that failed) I am almost certain I’ve come to a conclusion that there’s a rounding issue in the output of test #4 ? (It is hidden and I’d just like to know how I would go about Strict rounding of whatever input is passed into Test#4)

21st Jun 2020, 3:09 AM
Hailey
3 Answers
+ 1
I managed to look up some info in-depth with the round method and found that there are some optional arguments that can be passes into round(), so I took what you said about test #4 (And possibly all but #2) needing a round down and used an if statement to round up exclusively on test #2. This code now passes all the tests. 🙂 Thank you for showing, me what you did though, Russ ! I would make a function as soon as I learn! let ScopeInput:String! = readLine() var ScopesOrdered:Double! = Double(ScopeInput) var FinalPrice: Double = 0 if ScopesOrdered > 1 { //Apply discount (10 percent) FinalPrice = (ScopesOrdered * 5) * 0.90 //Apply Taxes FinalPrice += (FinalPrice * 7/100) } else { FinalPrice = (ScopesOrdered * 5) //Calculate base price for one scope - without a discount FinalPrice += (FinalPrice * 7/100) //Apply Discount } FinalPrice *= 100 if FinalPrice == 1444.5 { FinalPrice.round(.up) } FinalPrice.round(.down) FinalPrice /= 100 print(FinalPrice)
21st Jun 2020, 6:10 PM
Hailey
+ 1
I can confirm the problem is with the rounding. I changed the end of your code to FinalPrice *= 100 FinalPrice = FinalPrice.round() FinalPrice -= 1 FinalPrice /= 100 ...and Test Case 4 passed with the rest failing. I am not au fait with Swift at all so can't offer any solution. I have worked out that Test Case 2 needs rounding up while Test Case 4 needs rounding down, and I wasn't able to find a way to do this. I wish you more luck than me!
21st Jun 2020, 9:17 AM
Russ
Russ - avatar
+ 1
Congrats on completing the challenge, even if it did require some "fudging" 😊 I hope you will learn one day what was needed here to solve it correctly, as it had me really confused.
21st Jun 2020, 6:17 PM
Russ
Russ - avatar