Advertisement
Guest User

8. Figure of 4 Squares

a guest
Feb 16th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. function printFigure([num]) {
  2. num = Number(num);
  3. if (!(num >= 2 && num <= 200)){
  4. return
  5. }
  6.  
  7. let repeatPlus = `-`.repeat(num-2);
  8. let repeatSpace = ` `.repeat(num-2);
  9. let verticalLines = num % 2 == 0 ? num / 2 - 2 : num / 2 - 1;
  10. verticalLines = Math.floor(verticalLines);
  11.  
  12. if(num == 2)
  13. return console.log(`+${repeatPlus}+${repeatPlus}+`);
  14.  
  15. console.log(`+${repeatPlus}+${repeatPlus}+`);
  16. for (let i = 0; i < verticalLines; i++) {
  17. console.log(`|${repeatSpace}|${repeatSpace}|`)
  18. }
  19. console.log(`+${repeatPlus}+${repeatPlus}+`);
  20. for (let i = 0; i < verticalLines; i++) {
  21. console.log(`|${repeatSpace}|${repeatSpace}|`)
  22. }
  23. console.log(`+${repeatPlus}+${repeatPlus}+`);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement