Back to Freecodecamp

Challenge 98: Rectangle Count

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/68f6587287ad1f4ad39b0c84.md

latest1.1 KB
Original Source

--description--

Given two positive integers representing the width and height of a rectangle, determine how many rectangles can fit in the given one.

  • Only count rectangles with integer width and height.

For example, given 1 and 3, return 6. Three 1x1 rectangles, two 1x2 rectangles, and one 1x3 rectangle.

--hints--

countRectangles(1, 3) should return 6.

js
assert.equal(countRectangles(1, 3), 6);

countRectangles(3, 2) should return 18.

js
assert.equal(countRectangles(3, 2), 18);

countRectangles(1, 2) should return 3.

js
assert.equal(countRectangles(1, 2), 3);

countRectangles(5, 4) should return 150.

js
assert.equal(countRectangles(5, 4), 150);

countRectangles(11, 19) should return 12540.

js
assert.equal(countRectangles(11, 19), 12540);

--seed--

--seed-contents--

js
function countRectangles(width, height) {

  return width;
}

--solutions--

js
function countRectangles(width, height) {
  let count = 0;

  for (let w = 1; w <= width; w++) {
    for (let h = 1; h <= height; h++) {
      count += (width - w + 1) * (height - h + 1);
    }
  }

  return count;
}