Advertisement
vladovip

AirPollution_JS Fund_ ArrAdv_MoreEx

Feb 17th, 2022
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function airPollution(mapInfo, affectingForces) {
  2.  
  3.     let mapOfSofia = mapInfo.map(number => number.split(' ').map(Number))
  4.  
  5.     while (affectingForces.length > 0) {
  6.         let currentArr = affectingForces.shift()
  7.         let currentForce = currentArr.split(' ')[0]
  8.         let currentValue = Number(currentArr.split(' ')[1])
  9.  
  10.         switch (currentForce) {
  11.             case 'breeze':
  12.                 mapOfSofia = breeze(mapOfSofia, currentValue)
  13.                 break;
  14.             case 'gale':
  15.                 mapOfSofia = gale(mapOfSofia, currentValue)
  16.                 break;
  17.             case 'smog':
  18.                 mapOfSofia = smog(mapOfSofia, currentValue)
  19.                 break;
  20.             default:
  21.                 break;
  22.         }
  23.     }
  24.  
  25.     function breeze(arr, index) {
  26.         for (let col = 0; col < arr.length; col++) {
  27.             let currentParticle = arr[index][col];
  28.             if (currentParticle - 15 >= 0) {
  29.                 arr[index][col] -= 15
  30.             } else {
  31.                 arr[index][col] = 0
  32.             }
  33.         }
  34.         return arr
  35.     }
  36.  
  37.     function gale(arr, index) {
  38.         for (let row = 0; row < arr.length; row++) {
  39.             let currentParticle = arr[row][index];
  40.             if (currentParticle - 20 >= 0) {
  41.                 arr[row][index] -= 20;
  42.             } else {
  43.                 arr[row][index] = 0;
  44.             }
  45.         }
  46.         return arr
  47.     }
  48.  
  49.     function smog(arr, value) {
  50.         for (let row = 0; row < arr.length; row++) {
  51.             for (let col = 0; col < arr.length; col++) {
  52.                 arr[row][col] += value;
  53.             }
  54.         }
  55.         return arr
  56.     }
  57.  
  58.     let result = 'Polluted areas: '
  59.  
  60.     // FIRST option:
  61.     // mapOfSofia.forEach((line, rowIndex) => {
  62.     //     line.forEach((number, colIndex) => {
  63.     //       if (number >= 50) {
  64.     //         return (result += `[${rowIndex}-${colIndex}], `);
  65.     //       }
  66.     //     });
  67.     //   });
  68.  
  69. // Second option:
  70.      for (let rows = 0; rows < mapOfSofia.length; rows++) {
  71.        for (let cols = 0; cols < mapOfSofia.length; cols++) {
  72.            let currentValue = mapOfSofia[rows][cols];
  73.  
  74.              if (currentValue >= 50) {
  75.                 result += `[${rows}-${cols}], `
  76.             }
  77.         }
  78.      }
  79.  
  80.    
  81.  console.log(result === 'Polluted areas: ' ? result = 'No polluted areas' : result.slice(0, -2))
  82.  
  83. }
  84.  
  85. let result = airPollution (
  86.     ["5 7 2 14 4",
  87.         "21 14 2 5 3",
  88.         "3 16 7 42 12",
  89.         "2 20 8 39 14",
  90.         "7 34 1 10 24"],
  91.     ["breeze 1", "gale 2", "smog 35"]
  92.  
  93.  
  94. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement