Advertisement
3vo

Problem 6. Word or Number

3vo
Nov 2nd, 2022
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem 6. Word or Number
  2. //
  3. // Write a program that reads text from the console.
  4. // Check if this text is a number or a word.
  5. // If the text is a word print it reversed on the console.
  6. // If it is a number add 1 to it and print it.
  7. // The input is text on a single line (without intervals).
  8. // If the input is a word it won't contain any digits!
  9. // The output is like in the examples below.
  10. //     Examples:
  11. // input        output
  12. // good         doog
  13. // TA           AT
  14. // 32           33
  15. // 42.5         43.50
  16. // -1           0
  17.  
  18. // let input = ['good'];
  19. let input = ['43.50'];
  20.  
  21. let print = this.print || console.log;
  22. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  23.  
  24. //Gets the text
  25. let text = gets();
  26. //Text converted to number
  27. let num = Number(text);
  28. //Create an empty string that will host the new created string
  29. let newText = '';
  30.  
  31. //check if is not, not a number
  32. //if true: log the result(number) + 1
  33. if (!isNaN(num)) {
  34.     num += +1
  35.     console.log(num)
  36. } else {
  37. //creat reverse (decrementing) for loop and log out the result
  38.     for (let i = text.length - 1; i >= 0; i--) {
  39.         newText += text[i];
  40.     }
  41.     console.log(newText)
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement