Exercise Solutions: Data and VariablesΒΆ

  1. makeLine()

    Write a function makeLine(size) that returns a line with exactly size hashes.

    1
    2
    3
    4
    5
    6
    7
    function makeLine(size) {
       let line = '';
       for (let i = 0; i < size; i++) {
          line += '#';
       }
       return line;
    }
    

    Back to the exercises

  1. makeRectangle()

    Write a function makeRectangle(width, height) that returns a rectangle with the given width and height.

    1
    2
    3
    4
    5
    6
    7
    function makeRectangle(width, height) {
       let rectangle = '';
       for (let i = 0; i < height; i++) {
          rectangle += (makeLine(width) + '\n');
       }
       return rectangle.slice(0, -1);
    }
    

    Back to the exercises

  1. makeDownwardStairs()

    Write a function makeDownwardStairs(height) that prints the staircase pattern shown, with the given height.

    1
    2
    3
    4
    5
    6
    7
    function makeDownwardStairs(height) {
       let stairs = '';
       for (let i = 0; i < height; i++) {
          stairs += (makeLine(i+1) + '\n');
       }
       return stairs.slice(0, -1);
    }
    

    Back to the exercises

  1. makeIsoscelesTriangle()

    Write a function makeIsoscelesTriangle(height) that returns a triangle of the given height.

    1
    2
    3
    4
    5
    6
    7
    function makeIsoscelesTriangle(height) {
       let triangle = '';
       for (let i = 0; i < height; i++) {
          triangle += (makeSpaceLine(height - i - 1, 2*i + 1) + '\n');
       }
       return triangle.slice(0, -1);
    }
    

    Back to the exercises