Advertisement
bai_onzi

zad11 loops

Feb 18th, 2023
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let lines = +gets();
  2. let sum = 0;
  3. let resultStr = '';
  4. let counter = 0;
  5.  
  6.  
  7. let newLine = gets();
  8. if (!isNaN(newLine)) {
  9.     sum += +newLine;
  10.     counter++;
  11.  
  12. } else if (isNaN(newLine)) {
  13.     resultStr = `${resultStr}-${newLine}`;
  14. }
  15.  
  16. for (let i = 0; i < lines - 1; i++) {
  17.  
  18.     let nextLine = gets();
  19.     if (nextLine === '') {
  20.         continue;
  21.     }
  22.  
  23.     if (!isNaN(newLine) && !isNaN(nextLine)) {
  24.         sum += +nextLine;
  25.         counter++;
  26.     } else if (isNaN(newLine) && isNaN(nextLine)) {
  27.         resultStr = `${resultStr}-${nextLine}`;
  28.     } else if (!isNaN(newLine) && isNaN(nextLine)) {
  29.         console.log(sum);
  30.         sum = 0;
  31.         counter = 0;
  32.         resultStr = `${resultStr}-${nextLine}`
  33.     } else if (isNaN(newLine) && !isNaN(nextLine)) {
  34.         if (resultStr[0] === '-') {
  35.             console.log(resultStr.slice(1,));
  36.         } else {
  37.             console.log(resultStr);
  38.         }
  39.  
  40.         resultStr = '';
  41.         sum += +nextLine;
  42.     }
  43.     newLine = nextLine
  44. }
  45. if (resultStr[0] === '-') {
  46.     console.log(resultStr.slice(1,));
  47. } else if (resultStr.length != 0) {
  48.     console.log(resultStr);
  49. }
  50. if (sum != 0 || counter > 0) {
  51.     console.log(sum);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement