Skip to content

Instantly share code, notes, and snippets.

@McLarenCollege
Created May 12, 2021 08:57
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/a48d52e5b253ea07c097b5f2432d70ac to your computer and use it in GitHub Desktop.
Save McLarenCollege/a48d52e5b253ea07c097b5f2432d70ac to your computer and use it in GitHub Desktop.
Practice Challenge 9 Boxes Rows Solution
function boxesRows(boxes){
let result=[];
let max=0;
// find the maximum number
for(let i=0;i<boxes.length;i++){
if(boxes[i]>max){
max=boxes[i];
}
}
// push zeros in the result array equal to the max number
for(let i=0;i<max;i++){
result.push(0);
}
// iterate through the boxes and increment elements of the result array
for(let i=0;i<boxes.length;i++){
for(let j=0;j<boxes[i];j++){
result[j]++;
}
}
return result;
}
console.log(boxesRows([4,1,2,3,1,2]));
console.log(boxesRows([1,0,2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment