Advertisement
Guest User

Untitled

a guest
Jun 29th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function seizeTheFire(arr) {
  2.  
  3.     let water = Number(arr.pop());
  4.     let cells = [];
  5.     let effort = 0;
  6.     let currentCommand = arr.shift().split('#');
  7.  
  8.     while (currentCommand[0] !== undefined && water > 0) {
  9.         let splitCommand = currentCommand.shift().split(' ');
  10.         let temperature = splitCommand[0];
  11.         let value = Number(splitCommand[2]);
  12.  
  13.         switch (temperature) {
  14.             case 'High':
  15.                 putOutWater(value, temperature);
  16.                 break;
  17.  
  18.             case 'Medium':
  19.                 putOutWater(value, temperature);
  20.                 break;
  21.  
  22.             case 'Low':
  23.                 putOutWater(value, temperature);
  24.                 break;
  25.         }
  26.     }
  27.  
  28.     console.log('Cells:');
  29.  
  30.     for (let i = 0; i < cells.length; i++) {
  31.         console.log(`- ${cells[i]}`);
  32.     }
  33.  
  34.     console.log('Effort: ' + effort.toFixed(2) + '\n' + 'Total Fire: ' + cells.reduce(((a, b) => a + b), 0));
  35.  
  36.     function isValueValid(cell, temperature) {
  37.         if (temperature === 'High') {
  38.             if (cell >= 81 && cell <= 125) {
  39.                 return true;
  40.             }
  41.         } else if (temperature === 'Medium') {
  42.             if (cell >= 51 && cell <= 80) {
  43.                 return true;
  44.             }
  45.         } else if (temperature === 'Low') {
  46.             if (cell >= 1 && cell <= 50) {
  47.                 return true;
  48.             }
  49.         }
  50.  
  51.         return false;
  52.     }
  53.  
  54.     function putOutWater(cell, temperature) {
  55.         if (isValueValid(cell, temperature)) {
  56.             if (water >= cell) {
  57.                 cells.push(cell);
  58.                 effort += cell * 0.25;
  59.                 water -= cell;
  60.             }
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement