Finished the War Card Game? Post Your Screenshot Here!

This was a fun project. I added percentages and winner/tie labels. I also added a tie score label and a reset button.

The hardest part was trying to figure out how to display the decimal numbers correctly. I tried using tuncatingRemainder(Dividing by) to tell if the float number for the percentages was actually a whole number. It worked on most whole numbers but missed 60% and 40% to name a few. The problem was that lets say (3/5)*100 should equal 60. However the computer was calculating the answer to 60.000004. to get around this I had to round the float to one decimal and then compare the INT value to the rounded float value. (see below)

   // Convert Int values to Float values, calculate percent and format the output to look like "90.1%" or "90%"
    func calculatePercentage(_ score:Int, _ total:Int) -> String {
        let percent:Float
        let percentRounded:Float
        let answer:String
        
        // calculate percent and convert Integers to Float values
        percent = (Float(score) / Float(total)) * 100
        //Round percent to one decimal Note:  Rounding to one decimal was necessary because some values like 40 or 60 were being calculated as 60.000004 or 40.000004. so they were falling through to the else statement.
        percentRounded = round(10 * percent)/10
        
        if percentRounded == Float(Int(percent)) {
            // Calculated value is an integer(Whole Number), round to zero decimals
            answer = "\(String(format: "%.0f" , percent))%"
            
        }
        else {
            // Calculated value is not an integer(Decimal number), round to one decimal
            answer = "\(String(format: "%.1f" , percent))%"
            
        }
        return answer
2 Likes

Congrats on the new badge and welcome to the community!!
I like how you extended the app!

Also you would think (3/5)* 100 equals 60, but 3 and 5 were both defined as integers. This means your answer will be an integer, not a float.

How cool, thank you Chris! Enrolled in the course a few days ago and I am very impressed. Any other healthcare professionals in here? I watch videos in between patients and study during lunch and some evenings. Any other people at the very beginning like me? I am on Module 2 Lesson 4…

2 Likes

2 Likes

2 Likes

Badge granted! Congrats and welcome to the community. We’re so happy to know you enjoyed Chris’s tutorials. :slight_smile:

Congrats on your shiny new badge! Welcome to the forum. :slight_smile:

1 Like

Congrats on your shiny new badge and welcome to the forum! :slight_smile:

Thanks you so much @Katrina. Looking forward to learn more coding stuff . :grinning:

1 Like

2 Likes

Congrats on your new badge! And welcome to the forum! :slight_smile:



Finally completed the task…
I have add an extra feature that updates the score after 10 taps, that is, on the 11th tap, both the scores will become 0.
Thanks Chris for such an amazing tutorial…
@CodeWithChris

2 Likes

Awesome! Congrats on your new badge and welcome to the community. :slight_smile:

1 Like

Hey toothdoc, there are a ton of people just starting out! I’m really inspired by your dedication to making it happen even with a busy schedule. :clap:

Not yet done but 2 days remaining.

Also a healthcare professional —welcome to the community