|
| 1 | +//example 1 makeLine(size) |
| 2 | +function makeLine(size) { |
| 3 | + let line = ""; |
| 4 | + for (let i = 0; i < size; i++) { |
| 5 | + line += "#"; |
| 6 | + } |
| 7 | + return line; |
| 8 | +} |
| 9 | + |
| 10 | +//example 2 makeSquare(size) |
| 11 | +function makeSquare(size) { |
| 12 | + let square = ""; |
| 13 | + for (let i = 0; i < size; i++) { |
| 14 | + square += makeLine(size); |
| 15 | + if (i < size - 1) { |
| 16 | + square += "\n"; |
| 17 | + } |
| 18 | + } |
| 19 | + return square; |
| 20 | +} |
| 21 | + |
| 22 | +//example 3 makeRectangle(width, height) |
| 23 | +function makeRectangle(width, height) { |
| 24 | + let rectangle = ""; |
| 25 | + for (let i = 0; i < height; i++) { |
| 26 | + rectangle += makeLine(width); |
| 27 | + if (i < height - 1) { |
| 28 | + rectangle += "\n"; |
| 29 | + } |
| 30 | + } |
| 31 | + return rectangle; |
| 32 | +} |
| 33 | + |
| 34 | +//example 4 makeDownwardStairs(height) |
| 35 | +function makeDownwardStairs(height) { |
| 36 | + let stairs = ""; |
| 37 | + for (let i = 0; i < height; i++) { |
| 38 | + stairs += makeLine(i + 1) + "\n"; |
| 39 | + } |
| 40 | + return stairs.slice(0, -1); |
| 41 | +} |
| 42 | + |
| 43 | +//example 5 makeSpaceLine(numSpaces, numChars) |
| 44 | +function makeSpaceLine(numSpaces, numChars) { |
| 45 | + let spaces = " ".repeat(numSpaces); |
| 46 | + let chars = "#".repeat(numChars); |
| 47 | + return spaces + chars + spaces; |
| 48 | +} |
| 49 | + |
| 50 | +//example 6 makeIsoscelesTriangle(height) |
| 51 | +function makeIsoscelesTriangle(height) { |
| 52 | + let triangle = ""; |
| 53 | + for (let i = 0; i < height; i++) { |
| 54 | + triangle += makeSpaceLine(height - i - 1, 2 * i + 1) + "\n"; |
| 55 | + } |
| 56 | + return triangle.slice(0, -1); |
| 57 | +} |
| 58 | +//example 7 makeDiamond(height) |
| 59 | +function makeDiamond(height) { |
| 60 | + let diamond = makeIsoscelesTriangle(height); |
| 61 | + for (let i = height - 2; i >= 0; i--) { |
| 62 | + diamond += makeSpaceLine(height - i - 1, 2 * i + 1) + "\n"; |
| 63 | + } |
| 64 | + return diamond; |
| 65 | +} |
| 66 | + |
| 67 | +console.log("makeLine(5)"); |
| 68 | +console.log(makeLine(5)); |
| 69 | +console.log("\n"); |
| 70 | +console.log("makeSquare(5)"); |
| 71 | +console.log(makeSquare(5)); |
| 72 | +console.log("\n"); |
| 73 | +console.log("makeRectangle(5,3)"); |
| 74 | +console.log(makeRectangle(5, 3)); |
| 75 | +console.log("\n"); |
| 76 | +console.log("makeDownwardStairs(5)"); |
| 77 | +console.log(makeDownwardStairs(5)); |
| 78 | +console.log("\n"); |
| 79 | +console.log("makeSpaceline(3, 5)"); |
| 80 | +console.log(makeSpaceLine(3, 5)); |
| 81 | +console.log("\n"); |
| 82 | +console.log("makeIsoscelesTriangle(5)"); |
| 83 | +console.log(makeIsoscelesTriangle(5)); |
| 84 | +console.log("\n"); |
| 85 | +console.log("makeDiamond(5)"); |
| 86 | +console.log(makeDiamond(5)); |
0 commit comments