Advertisement
VeselaVideva

Arrays - Exercise - 10. Ladybugs

Nov 5th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ladybugs(tokens = []) {
  2.     let fieldSize = +tokens[0];
  3.     let ladyBugs = [];
  4.  
  5.     for (let i = 0; i < fieldSize; i++) { // Initialize field
  6.         ladyBugs[i] = 0;
  7.     }
  8.  
  9.     let initialIndexes = tokens[1].split(' ').map(index => +index);
  10.  
  11.     for (let i = 0; i < ladyBugs.length; i++) { // Fill bugs on field
  12.         if (initialIndexes.includes(i)) {
  13.             ladyBugs[i] = 1;
  14.         }
  15.     }
  16.  
  17.     //------------------------------------------------------------------------------------------
  18.     for (let i = 2; i < tokens.length; i++) {
  19.         let command = tokens[i].split(' ');
  20.  
  21.         if (command[0].toLowerCase() !== 'end') {
  22.             let index = +command[0];
  23.  
  24.             if (index < 0 || index >= ladyBugs.length) {   // If index is inside the field
  25.                 continue;
  26.             }
  27.  
  28.             if (ladyBugs[index] === 0) {  // cell is empty (NO Lady Bug)
  29.                 continue;
  30.             }
  31.  
  32.             //------------- Fly like the wind -------------
  33.             let direction = command[1];
  34.             let flyLength = +command[2];
  35.  
  36.             if (flyLength < 0) { // If flyLength is negative, change direction, and make it positive
  37.                 flyLength = Math.abs(flyLength);
  38.                 switch (direction) {
  39.                     case 'right':
  40.                         direction = 'left';
  41.                         break;
  42.                     case 'left':
  43.                         direction = 'right';
  44.                         break;
  45.                 }
  46.             }
  47.  
  48.             ladyBugs[index] = 0; // Lift off in 3...2...1
  49.             let isBugFlying = true;
  50.  
  51.             while (isBugFlying) {
  52.                 switch (direction) {
  53.                     case 'right':
  54.                         if (index + flyLength >= ladyBugs.length) { // Lady Bug flew away (outside field)
  55.                             isBugFlying = false;
  56.                         } else {
  57.                             if (ladyBugs[index + flyLength] === 0) { // is cell empty (no Lady Bug at index)
  58.                                 ladyBugs[index + flyLength] = 1; // Lady Bug landed
  59.                                 isBugFlying = false;
  60.                             } else {
  61.                                 isBugFlying = true; // Lady Bug continues to fly
  62.                                 index += flyLength;
  63.                             }
  64.                         }
  65.                         break;
  66.                     case 'left':
  67.                         if (index - flyLength < 0) { // Lady Bug flew away (outside field)
  68.                             isBugFlying = false;
  69.                         } else {
  70.                             if (ladyBugs[index - flyLength] === 0) { // is cell empty (no Lady Bug at index)
  71.                                 ladyBugs[index - flyLength] = 1; // Lady Bug landed
  72.                                 isBugFlying = false;
  73.                             } else {
  74.                                 isBugFlying = true; // Lady Bug continues to fly
  75.                                 index -= flyLength;
  76.                             }
  77.                         }
  78.                         break;
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     console.log(ladyBugs.join(' '));
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement