Spocoman

06. Building (solutions with or not ternary operator)

Dec 30th, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function building(input) {
  2.     let floor = parseInt(input[0]);
  3.     let volume = parseInt(input[1]);
  4.  
  5.     for (let f = floor; f > 0; f--){
  6.         let currentFloor = ``;
  7.         for (let v = 0; v < volume; v++){
  8.             if (floor === f){
  9.                 currentFloor += `L${f}${v} `;
  10.             } else{
  11.                 if (f % 2 === 0) {
  12.                     currentFloor += `O${f}${v} `
  13.                  } else {
  14.                      currentFloor += `A${f}${v} `;
  15.                  }
  16.             }
  17.         }
  18.         console.log(currentFloor);
  19.     }
  20. }
  21.  
  22. Решение с тернарен оператор:
  23.  
  24. function building(input) {
  25.     let floor = parseInt(input[0]);
  26.     let volume = parseInt(input[1]);
  27.  
  28.     for (let f = floor; f > 0; f--){
  29.         let currentFloor = ``;
  30.         for (let v = 0; v < volume; v++){
  31.            currentFloor += `${floor === f ? 'L': f % 2 === 0 ? 'O' : 'A'}${f}${v} `;      
  32.         }
  33.         console.log(currentFloor);
  34.     }
  35. }
Add Comment
Please, Sign In to add comment