A different loop

Hello,

So, I was just trying to follow the challenge and I did the loop differently. My logic was:

    int32 HiddenWordLength = MyHiddenWord.length();
    // First do the loop for Bulls, if the guess character position is the same as the 
    // hidden word character position add to the bull count
	for (int32 GuChar = 0; GuChar < HiddenWordLength; GuChar++) 
	{
		if (Guess[GuChar] == MyHiddenWord[GuChar])
		{
			ThisBullCowCount.Bulls++;
		}
   // If no bulls are found then do the loop for the cows. 
   // For the cows the Hidden Word character position 
   // has to match the current guessed character position 
		else {
				for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++) 
						{
					if (Guess[GuChar] == MyHiddenWord[MHWChar]) 
							{
							ThisBullCowCount.Cows++;
							}
						}
		}
	}
   // If no bulls or cows have been found nothing will be added
	return ThisBullCowCount;

Is there a problem with this approach? Is it clear?

Thanks for any help from anyone.

Not really though this does mean you are potentially comparing the letters an additional time in your case. As if the first comparison is false then the first instance of the inner for loop will be checking the same thing.

Realistically the compiler will most likely optimise this loop anyway

Privacy & Terms