WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Checkpoint 4 Solution

Forums > 100 Days of SwiftUI

I'm confused why we would want to run the loop 100 times if it doesn't need to.

For example, if the square root we are looking for is 15 (there is no perfect square number that will get us to 15) why would we continue to run the loop after that.

I did an exhaustive loop (1...) and returned i if i multipied by i == number but then I also did an else if check to see if i multipled by i was greater than number, and if so, to break from the loop.

I feel like this incorporates more of what we learned and should be the actual solution instead of hardcoding in values.

Some feedback too while I am here... I wish the tests in each lesson would focus more on what I've learned in that lesson and not try to trick me with silly typo errors here and there as that's not helping to solidify understanding in that particular lesson in my opinion...

Edit: Also thank you for this course I'm not trying to be ungrateful I just want to point out my feedback.

   

You might want to look at this post Need some help with Checkpoint 4

   

gthang writes:

I wish the tests in each lesson would focus more on what I've learned in that lesson and not try to trick me with silly typo errors here and there as that's not helping to solidify understanding in that particular lesson

Maybe before your time? Do you remember the scene in the movie "The Karate Kid" where Mr Miyagi offers to teach Daniel LaRusso karate? But first Daniel must wash and wax the car? He had to "wax-on wax-off" 1000 times? Next he had to paint the fence. "Brush up, brush down" all day long, several thousands of times.

Daniel waxed and waxed. Then he painted and painted some more. Then he threw a fit, demanding to know when he would start learning karate and stop with the personal chores! You remember this scene?

Of course the reveal is that Mr Miyagi was teaching him moves and getting them ingrained in muscle memory. Wax-on, Wax-off is a defensive move. Very important to learning karate.

Catching "silly typo errors here and there" is Hacking With Swift's version of "Wax-On, Wax-Off".

You need this skill! You will be doing this tens of thousands of times on your 100 day journey. This skill is just as important as learning feature sets.

   

gthang points out:

I'm confused why we would want to run the loop 100 times if it doesn't need to. I did an exhaustive loop (1...) and returned i if i multipied by i == number but then I also did an else if check to see if i multipled by i was greater than number, and if so, to break from the loop.

You have advanced insight into problem solving. Nice! This will help you later on.

The point of earlier lessons isn't focused so much on writing precise, tight, perfect code. It's about learning concepts.

You'll see this in later episodes, too. @twostraws will walk you down a design path full of nice working code. Then, BAM!, he'll erase a 10 line method you meticulously copied line by line and show, instead, a one line version producing the same results. You may ask why he didn't start with the one-line solution?

This is because it's good practice to use your brain to think through the multiple steps to solve a logic problem. Then work through the steps to reduce the complexity. Then, finally, using a newly introduced feature to accomplish the same task with one line of code.

   

Hey thanks for the insights guys!

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.