Skip to content

Instantly share code, notes, and snippets.

@knrt10
Last active May 11, 2018 05:44
Show Gist options
  • Save knrt10/646bcfa7df5523fefd6ecd98daa60bb0 to your computer and use it in GitHub Desktop.
Save knrt10/646bcfa7df5523fefd6ecd98daa60bb0 to your computer and use it in GitHub Desktop.
See correct wrods
/**
* @function color
* @param {String} quote
* @param {String} stringTyped
*/
function color (quote, stringTyped) {
let colouredString = ''
let wrongInput = false
const quoteLetters = quote.split('')
const typedLetters = stringTyped.split('')
for (let i = 0; i < typedLetters.length; i++) {
// if a single mistake,
// the rest of the coloured string will appear red
if (wrongInput) {
colouredString += chalk.bgRed(quoteLetters[i])
continue
}
if (typedLetters[i] === quoteLetters[i]) {
wrongInput = false
colouredString += chalk.green(quoteLetters[i])
if (quote === stringTyped) {
gameEnd = true
}
} else {
wrongInput = true
colouredString += chalk.bgRed(quoteLetters[i])
}
}
return colouredString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment