Advertisement
plamen27

Figure of 4 squares

Nov 27th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. function generateFigure([size]) {
  2. size = Number(size);
  3. let output = '';
  4.  
  5. if (size % 2 == 0) {
  6. let totalRows = size - 1;
  7. let totalCols = size * 2 - 1;
  8. let numberOfRows = (totalRows - 3) / 2;
  9. let numberOfCols = (totalCols - 3) / 2;
  10.  
  11. let majorLine = `+${'-'.repeat(numberOfCols)}+${'-'.repeat(numberOfCols)}+\n`;
  12. let otherLine = `|${' '.repeat(numberOfCols)}|${' '.repeat(numberOfCols)}|\n`;
  13. output += majorLine;
  14. for (let row = 0; row < numberOfRows; row++) {
  15. output += otherLine;
  16. }
  17.  
  18. output += majorLine;
  19. for (let row = 0; row < numberOfRows; row++) {
  20. output += otherLine;
  21. }
  22.  
  23. output += majorLine;
  24.  
  25. if (size == 2) {
  26. return majorLine;
  27. }
  28. } else {
  29. let totalRows = size;
  30. let totalCols = size * 2 - 1;
  31. let numberOfRows = (totalRows - 3) / 2;
  32. let numberOfCols = (totalCols - 3) / 2;
  33.  
  34. let majorLine = `+${'-'.repeat(numberOfCols)}+${'-'.repeat(numberOfCols)}+\n`;
  35. let otherLine = `|${' '.repeat(numberOfCols)}|${' '.repeat(numberOfCols)}|\n`;
  36. output += majorLine;
  37. for (let row = 0; row < numberOfRows; row++) {
  38. output += otherLine;
  39. }
  40.  
  41. output += majorLine;
  42. for (let row = 0; row < numberOfRows; row++) {
  43. output += otherLine;
  44. }
  45.  
  46. output += majorLine;
  47. }
  48.  
  49. return output;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement