Advertisement
3vo

Problem 10. Word or Number 2

3vo
Nov 1st, 2022 (edited)
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Problem 10. Word or Number 2
  2.  
  3. // You are given an integer number n and then n new lines of text: numbers or words (see Problem 6). This time when we have a word we concatenate it with the previous words with a dash - between them. If we have a number we add it to all previous numbers.
  4.  
  5. // The input starts by the number n (alone in a line) followed by n lines, each holding a text without a space.
  6. // Again all words contain no digits.
  7. // The output is like in the examples below.
  8. // On the first line there are all words concatenated with - between them or no words if there were no words in the input.
  9. // On the second line is the sum of all the numbers or 0 if there were no numbers.
  10.  
  11.  
  12. // Examples:
  13. // input            output
  14. // 5
  15. // 1
  16. // go
  17. // 1
  18. // there
  19. // 5                go-there
  20. //                  7
  21.  
  22. //NO text input
  23. let input = [
  24.     '3',
  25.     '1',
  26.     '1',
  27.     '7',
  28. ];
  29.  
  30. //with text input
  31. // let input = [
  32. //     '5',
  33. //     '1',
  34. //     'go',
  35. //     '1',
  36. //     'there',
  37. //     '5',
  38. // ];
  39.  
  40. //no numbers input
  41. // let input = [
  42. //     '3',
  43. //     'try',
  44. //     'google',
  45. //     'it'
  46. // ];
  47.  
  48. let print = this.print || console.log;
  49. let gets = this.gets || ((arr, index) => () => arr[index++])(input, 0);
  50.  
  51. let length = +gets();
  52. let char = '';
  53. let text = '';
  54. let num = 0;
  55.  
  56. for (let i = 0; i < length; i++) {
  57.     char = gets();
  58.     if (!isNaN(char)) {
  59.         num += +char;
  60.     } else if (isNaN(char)) {
  61.         text += char + '-';
  62.     }
  63. }
  64. if (text === '') {
  65.     console.log('no words');
  66. }
  67. console.log(text.slice(0, -1));
  68. console.log(num);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement