Guest User

Untitled

a guest
Mar 24th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. let readline = require('readline')
  2. let rl = readline.createInterface(process.stdin, process.stdout)
  3.  
  4. function ageIsValid (x) {
  5. if ((parseInt(x) > 99) || isNaN(x)) {
  6. return false
  7. }
  8. else {
  9. return true
  10. }
  11. }
  12.  
  13. function askBirthday (x) {
  14. rl.question(`Did you turn ${x} this year? (Y/N)\n --> `, function (answer) {
  15. let currentYear = ((new Date()).getFullYear())
  16. if (answer === 'Y') {
  17. console.log('You were born in ' + (currentYear - x))
  18. rl.close()
  19. } else if (answer === 'N') {
  20. console.log('You were born in ' + (currentYear - x - 1))
  21. rl.close()
  22. } else {
  23. console.log('ERROR: Please respond with Y or N\n')
  24. askBirthday(x)
  25. }
  26. })
  27. }
  28.  
  29. function ageApp () {
  30. rl.question('How old are you?\n --> ', function (age) {
  31. if (ageIsValid(age)) {
  32. askBirthday(age)
  33. } else {
  34. console.log('ERROR: Enter a correct age.')
  35. ageApp()
  36. }
  37. })
  38. }
  39.  
  40. console.log('<------ AGE APP <3 Made with love at the WCS-------->')
  41. ageApp()
Add Comment
Please, Sign In to add comment