You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Diamonds.js 811B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. function makeLine(size) {
  2. let line = '';
  3. for (let i = 0; i < size; i++) {
  4. line += '#';
  5. }
  6. return line;
  7. }
  8. function makeSpaceLine(numSpaces, numChars) {
  9. let spaceLine = '';
  10. for (let i = 0; i < numSpaces; i++) {
  11. spaceLine += '_';
  12. }
  13. return spaceLine + makeLine(numChars) + spaceLine;
  14. }
  15. function makeIsoscelesTriangle(height) {
  16. let triangle = '';
  17. for (let i = 1; i <= height; i++) {
  18. triangle += makeSpaceLine(height - i, 2 * i - 1) + '\n';
  19. }
  20. return triangle.trimEnd();
  21. }
  22. function makeDiamonds(height) {
  23. let triangle = '';
  24. let diamond = '';
  25. for (let i = 1; i <= height; i++) {
  26. triangle = makeIsoscelesTriangle(height);
  27. }
  28. for (let j = height; j > 0; j--) {
  29. diamond += makeSpaceLine(height - j, 2 * j - 1) + '\n';
  30. }
  31. return triangle + '\n' + diamond;
  32. }
  33. console.log(makeDiamonds(5));