Guest User

Untitled

a guest
May 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. let info = {init: false};
  2. let run = word => {
  3. info = {
  4. word: word,
  5. letters: word.split(''),
  6. discoveries: [],
  7. correct: 0,
  8. errors: 0,
  9. limit_errors: 5,
  10. }
  11. render();
  12. }
  13. let attempt = (letter) => {
  14. let result = getLetter(info.letters, letter);
  15. if(!result){
  16. console.log("errou");
  17. return checkDefeat();
  18. }
  19. console.log("acertou");
  20. checkWin(result);
  21. }
  22. getLetter = (arr, letter) => {
  23. let data = arr.filter(arr_letter => letter == arr_letter)
  24. if(!data.length) return false;
  25. console.log(data);
  26. return {
  27. letter: data[0],
  28. length: data.length
  29. }
  30. }
  31. let checkWin = result => {
  32. info.correct += result.length;
  33. if(info.correct == info.letters.length){
  34. console.log('win');
  35. }
  36. info.discoveries.push(result.letter);
  37. render();
  38. }
  39. checkDefeat = () => {
  40. info.errors += 1;
  41. if(info.errors > info.limit_errors){
  42. console.log('lost');
  43. }
  44. }
  45. render = () => {
  46. let str = '';
  47. for(let i = 0, lim = info.word.length; i < lim; i++){
  48. str += info.discoveries.indexOf(info.word[i]) == -1 ? "_ ": info.word[i] + " ";
  49. }
  50. console.log(str);
  51. }
  52.  
  53. // to start the play use function run and send the word what do you want to play
  54. //run('wordtoplay');
  55.  
  56. // to play use function attempt send the letters input
  57. //attempt('o');
Add Comment
Please, Sign In to add comment