Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Last active March 17, 2022 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save McLarenCollege/39a8ae87fdff56e637e56b143abe3cf1 to your computer and use it in GitHub Desktop.
Save McLarenCollege/39a8ae87fdff56e637e56b143abe3cf1 to your computer and use it in GitHub Desktop.
Count the number of boxes in each row

There is an array of boxes where each element of an array represents the number of boxes stacked on top of each other in that column. For eg. boxes=[4,1,2,3,2,1].

This is a row of boxes where the first column of boxes has 4 boxes stacked on top of each other. The second column has 1 box, the third column has 2 boxes stacked on top of each other and the fourth column has 3 boxes stacked on top of each other and so on. So the boxes look like this:

              B
              B     B
              B   B B B
              B B B B B B

Your task is to write a function that accepts this array and returns an array containing the number of boxes present in each row starting from the bottom. For eg. the result for the above eg. would be [6,4,2,1] as there are 6 boxes in the bottom-most row, 4 boxes in the row above the bottom row , 2 boxes in the row above it and 1 box in the topmost row.

Hence.

console.log(boxesInRows([4,1,2,3,2,1])) // returns [6,4,2,1]

CODE TEMPLATE


function boxesInRows(boxes){
// write your code here
}
console.log(boxesInRows([4, 1, 2, 3, 2, 1]));// returns [6, 4, 2, 1]
console.log(boxesInRows([4, 0, 3, 1, 2]));// returns [4, 3, 2, 1]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment