Guest User

Air Pollution

a guest
Jun 22nd, 2020
426
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 METHOD
  61.     mapOfSofia.forEach((line, rowIndex) => {
  62.         line.forEach(number => {
  63.             let colIndex = line.indexOf(number)
  64.             if (number >= 50) {
  65.                 return result += `[${rowIndex}-${colIndex}], `
  66.             }
  67.         })
  68.     })
  69.     // SECOND METHOD
  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.     console.log(result === 'Polluted areas: ' ? result = 'No polluted areas' : result.slice(0, -2))
  81.  
  82. }
  83. let result = airPollution(
  84.     ["5 7 2 14 4",
  85.         "21 14 2 5 3",
  86.         "3 16 7 42 12",
  87.         "2 20 8 39 14",
  88.         "7 34 1 10 24"],
  89.     ["breeze 1", "gale 2", "smog 35"]
  90.  
  91.  
  92. )
Add Comment
Please, Sign In to add comment