Guest User

Untitled

a guest
Nov 13th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. let readlineSync = require('readline-sync');
  2.  
  3. console.log("Welcome to Heads or Tails!")
  4.  
  5. function getUserChoice(values) {
  6.  
  7. userChoice = readlineSync.question('heads or tails');
  8.  
  9. while(values.includes(userChoice)) {
  10. console.log(`${userChoice} is not a valid answer.`)
  11. userChoice = readlineSync.question('Choose between heads or tails: ');
  12. }
  13.  
  14. console.log(userChoice);
  15. return userChoice;
  16. }
  17.  
  18. function getComputerChoice(values) {
  19.  
  20. let index = Math.floor(Math.random() * 2);
  21. let computerChoice = values[index];
  22.  
  23. return computerChoice;
  24. }
  25.  
  26.  
  27.  
  28. function start() {
  29. let choices = ['head', 'tail',];
  30. let score = {user: 0, computer: 0};
  31. let userChoice;
  32. let computerChoice;
  33. let continuePlaying = true;
  34.  
  35. while(continuePlaying === true) {
  36.  
  37.  
  38. userChoice = getUserChoice(choices);
  39. computerChoice = getComputerChoice(choices);
  40. console.log(userChoice, computerChoice);
  41.  
  42. if(userChoice === computerChoice) {
  43. console.log("AW better luck next time!");
  44. } else if (userChoice === 'tails' && computerChoice === 'heads' || userChoice === 'heads' && computerChoice === 'tails') {
  45. console.log("User Won, Congrats!!");
  46. score.user++;
  47. } else {
  48. console.log("Computer won, you lost!");
  49. score.computer++;
  50. }
  51.  
  52. console.log(`User has ${score.user} points, computer has ${score.computer} points`);
  53.  
  54.  
  55. continuePlaying = readlineSync.question('Do you want to play again? ((Y)es/(N)o)');
  56.  
  57. if(continuePlaying.toLowerCase() !== 'y') {
  58. continuePlaying = false;
  59. } else {
  60. continuePlaying = true;
  61. }
  62. }
  63. console.log("Thanks for playing, come back soon!");
  64. }
  65.  
  66. start();
Add Comment
Please, Sign In to add comment