Exercise Solutions: Data and VariablesΒΆ

  1. makeLine()

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

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

    Back to the exercises

  1. makeRectangle()

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

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

    Back to the exercises

  1. makeDownwardStairs()

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

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

    Back to the exercises

  1. makeIsoscelesTriangle()

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

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

    Back to the exercises