Created
May 12, 2021 08:57
-
-
Save McLarenCollege/a48d52e5b253ea07c097b5f2432d70ac to your computer and use it in GitHub Desktop.
Practice Challenge 9 Boxes Rows Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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