kstoyanov

02. Air Pollution js exam

Aug 6th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(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.     mapOfSofia.forEach((line, rowIndex) => {
  61.         line.forEach((number, colIndex) => {
  62.         if (number >= 50) {
  63.             return (result += `[${rowIndex}-${colIndex}], `);
  64.         }
  65.         });
  66.     });    
  67.  
  68.     console.log(result === 'Polluted areas: ' ? result = 'No polluted areas' : result.slice(0, -2))
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment