Skip to content

Instantly share code, notes, and snippets.

@leonardovff
Last active May 16, 2018 04:44
Show Gist options
  • Save leonardovff/27cfcd7db73d1e4040f03b4dac85d2ad to your computer and use it in GitHub Desktop.
Save leonardovff/27cfcd7db73d1e4040f03b4dac85d2ad to your computer and use it in GitHub Desktop.
let info = {init: false};
let run = word => {
info = {
word: word,
letters: word.split(''),
discoveries: [],
correct: 0,
errors: 0,
limit_errors: 5,
}
render();
}
let attempt = (letter) => {
let result = getLetter(info.letters, letter);
if(!result){
console.log("errou");
return checkDefeat();
}
console.log("acertou");
checkWin(result);
}
getLetter = (arr, letter) => {
let data = arr.filter(arr_letter => letter == arr_letter)
if(!data.length) return false;
console.log(data);
return {
letter: data[0],
length: data.length
}
}
let checkWin = result => {
info.correct += result.length;
if(info.correct == info.letters.length){
console.log('win');
}
info.discoveries.push(result.letter);
render();
}
checkDefeat = () => {
info.errors += 1;
if(info.errors > info.limit_errors){
console.log('lost');
}
}
render = () => {
let str = '';
for(let i = 0, lim = info.word.length; i < lim; i++){
str += info.discoveries.indexOf(info.word[i]) == -1 ? "_ ": info.word[i] + " ";
}
console.log(str);
}
// to start the play use function run and send the word what do you want to play
//run('wordtoplay');
// to play use function attempt send the letters input
//attempt('o');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment