So, I'm currently learning Swift (with no programming experience at all) with Swift Playgrounds.
I am currently on Learn To Code 2 at the level Round Up the Switches.
My solution for this level was:
However, I noticed that this solution doesn't work. Byte didn't even start to move.
I had to replace the And (&&) operator with the Or (||) operator in the while loop, but why do I have to do that?
I just wanted my while loop to be executed when both conditions are true. Both conditions were met, right?
Thank you very much in advance.
I am currently on Learn To Code 2 at the level Round Up the Switches.
My solution for this level was:
Swift:
var numberOfGems = 0
var numberOfSwitches = 0
func checkTile() {
if isOnGem {
collectGem()
numberOfGems += 1
}
if isOnClosedSwitch {
toggleSwitch()
numberOfSwitches += 1
}
}
func checkWhereToGo() {
if isBlocked {
turnRight()
}
}
while numberOfGems != numberOfSwitches && numberOfSwitches == 0 {
moveForward()
checkTile()
checkWhereToGo()
}
However, I noticed that this solution doesn't work. Byte didn't even start to move.
I had to replace the And (&&) operator with the Or (||) operator in the while loop, but why do I have to do that?
Swift:
var numberOfGems = 0
var numberOfSwitches = 0
func checkTile() {
if isOnGem {
collectGem()
numberOfGems += 1
}
if isOnClosedSwitch {
toggleSwitch()
numberOfSwitches += 1
}
}
func checkWhereToGo() {
if isBlocked {
turnRight()
}
}
while numberOfGems != numberOfSwitches || numberOfSwitches == 0 {
moveForward()
checkTile()
checkWhereToGo()
}
I just wanted my while loop to be executed when both conditions are true. Both conditions were met, right?
Thank you very much in advance.