I ended up looking up the answer for this after trying on my own. I understand whats going on all the way up until the while loop. I followed along on python tutor as well, but I cant wrap my head around on how the while loop after its first loop, that it doesn’t go straight to dog after pushing cat into the array the first time.
I would expect the loop to count down from 2 (count down because of the counter -= 1) but instead of starting back at the top of the object to push ‘cat’ in again, it would continue to work down the object with the next value which is ‘dog’ and then push that in…and THEN it would go back up to the top of the object again and push ‘cat’ into the array and then stop because then the counter would be finished.
so in other words why would the output be [‘cat’, ‘cat’, ‘dog’] and NOT [‘cat’, ‘dog’, ‘cat’]??
function arrayBuilder(obj) {
// ADD CODE HERE
const array = []
for (let key in obj) {
let counter = obj[key]
while (counter > 0) {
array.push(key)
counter -= 1
}
}
return array
}
// Uncomment these to check your work!
console.log(arrayBuilder({'cats': 2, 'dogs': 1})); // => ['cats', 'cats', 'dogs']