Skip to content

Instantly share code, notes, and snippets.

@dapperAuteur
Created July 29, 2019 15:33
Show Gist options
  • Save dapperAuteur/d0a39663c03b19491c50fda629bb6b81 to your computer and use it in GitHub Desktop.
Save dapperAuteur/d0a39663c03b19491c50fda629bb6b81 to your computer and use it in GitHub Desktop.
An ok time to use a nested for loop.
/* This code sample exemplifies an important aspect of your coding philosophy for many reasons.
* In most cases it's not a good idea to nest for loops.
* In this unique case it's ok because the object(s) being looped will always have a length of 4.
* My coding philosophy is similar to my life philosophy, everything depends on the event it's applied to.
* There are few hard and fast rules. Read the room and make a decision.
* Another way in which this code sample exemplifies my coding philosophy is that the function rewards the player for effort.
* This code is part of a game that asks the player to guess a four letter word. The player may receive points for wrong answers.
*
*
*/
const guessNotWinningWord = (currentGame, guessLowerCase) => {
console.log(currentGame);
let bulls = 0;
let cows = 0;
let winningWord = currentGame.winningWord;
let guess = guessLowerCase;
console.log(winningWord);
// console.log(guess);
let arr_guess = guess.split("");
let arr_word = winningWord.split("");
let message = `${guess} is NOT the Word`;
let scored = 0;
for (var i = 0; i < arr_guess.length; i++) {
for (var j = 0; j < arr_word.length; j++) {
if (arr_guess[i] === arr_word[j]) {
if (i === j) {
bulls++;
scored += 100;
arr_guess[i] = "0";
arr_word[j] = "1";
}
}
if (arr_guess[i] === arr_word[j]) {
cows++;
scored += 50;
arr_guess[i] = "0";
arr_word[j] = "1";
}
}
}
let userDidNotWinGame = {
cows,
bulls,
guess,
message,
scored
};
// console.log(userDidNotWinGame);
return userDidNotWin(userDidNotWinGame);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment