In the ‘Debugging 101’ video, the comments imply that we want to…
- Loop through each guess character.
- Loop through each hidden word character.
- Check if the guess character (as defined in the first loop) is equal to the hidden word character (as defined in the second loop).
- if true, check if the indexes are equal.
- If they are equal, increment Bulls. If not, increment cows.
However, with the way his iterators are named, he is saying that we want to…
- Loop through each hidden word character.
- Loop through each guess character.
Line 47 is fine because he’s using the corresponding indices for Guess and MyHiddenWord. The issue just lies with the order in which he named the variables.
The following image shows how I believe it should be named. Ignore the other small changes I’ve made, just focus on the underlined variables.
As an example with the corrected code, here is what it would look like if the guess is “and” and the word is “ant”.
- what is Guess[0]?
- it’s ‘a’.
- does it match ‘a’ from “ant”? Yes! It’s in the same spot, so add a bull.
- does it match ‘n’ from “ant”? No.
- does it match ‘t’ from “ant”? No.
- it’s ‘a’.
- what is Guess[1]?
- it’s ‘n’.
- does it match ‘a’ from “ant”? No.
- does it match ‘n’ from “ant”? Yes! It’s in the same spot, so add a bull.
- does it match ‘t’ from “ant”? No.
- it’s ‘n’.
- what is Guess[2]?
- it’s ‘d’.
- does it match ‘a’ from “ant”? No.
- does it match ‘n’ from “ant”? No.
- does it match ‘t’ from “ant”? No.
- it’s ‘d’.
In conclusion, the code works fine but it doesn’t match the logic that he introduced with his pseudocode. Feel free to let me know if you disagree or if I’ve made a mistake.