Posts

Post not yet marked as solved
2 Replies
2k Views
Hi,I an a beginner in coding so I'm starting to learn with Swift Playground. I have a question with the Nesting Loops exercise in Swift Playground. I don't understand what's wrong with my answer for the exercise and I don't understand the answer given for the exercise.This was my answer to the Nesting Loops exercise:While !isBlocked { While isOnGem { turnLeft() collectGem() } moveForward()}The result I got with this response is the character stopping on the 2nd gem, facing forward and not collecting gem. It turns left and collects the gem at the 1st spot. Why doesn't it do the same thing with the later gems after that? I know While Loops are supposed to keep going but the character is stopping before finishing collecting all the gems.Maybe there is something wrong with my thinking? I don't know. I wrote this code so that the character keeps on the outer loop until it encounters the inner loop (gem). Once the inner loop is completed, doesn't it go back to following the outer loop again?Also, the supposed correct answer doesn't make sense to me:While !isBlocked { While !isOnGem { moveForward() } collectGem() turnLeft()}Since the way is not blocked for several tiles in the first row, is Byte (the character) supposed to be collecting gems and turning left at every single tile??? I'm baffled by this.
Posted
by Voyanimg.
Last updated
.