Advertisement
Guest User

7th Exercise - Building | Nested Loops

a guest
Nov 20th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Building */
  2.  
  3. function building(input) {
  4.     let floors = Number(input.shift());
  5.     let rooms = Number(input.shift());
  6.  
  7.     let lApartments = ''; /* Variable for L Apartments */
  8.     let oOffices = ''; /* Variable for O Offices */
  9.     let aApartments = ''; /* Variable for A Apartments */
  10.  
  11.     for (let i = floors; i >= 1; i--) {
  12.         for (let r = 0; r < rooms; r++) {
  13.             if (i % 2 === 0) { /* offices O */
  14.                 if (i === floors || floors === 1) { /* Checking if i = the last floor OR if we have only one floor */
  15.                     lApartments += `L${i}${r} `; /* Concatenating */
  16.                     if (r === rooms - 1) { /* If r is equal to (rooms - 1), then we print the results on the console and continue with the loop */
  17.                         console.log(lApartments);
  18.                     }
  19.                 } else {
  20.                     oOffices += `O${i}${r} `; /* Concatenating */
  21.                     if (r === rooms - 1) {
  22.                         console.log(oOffices);
  23.                     }
  24.                 }
  25.    
  26.             } else { /* apartments A */
  27.                 if (i === floors || floors === 1) {
  28.                     lApartments += `L${i}${r} `; /* Concatenating */
  29.                     if (r === rooms - 1) {
  30.                         console.log(lApartments);
  31.                     }
  32.                 } else {
  33.                     aApartments += `A${i}${r} `; /* Concatenating */
  34.                     if (r === rooms - 1) {
  35.                         console.log(aApartments);
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.  
  41.         /* Refreshing the Values of the Both Variables
  42.         Because We Want Them on New Lines Every for Every
  43.         New Floor */
  44.  
  45.         oOffices = '';
  46.         aApartments = '';
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement