Empty and Refill the Basket
JavaScript
20 pts
Empty and Refill the Basket
Ruta’s shopping basket is a const array. Write a function addAll(target, items) that pushes every item of items onto the end of target (use forEach and push). Then:
- empty
basketwithbasket.length = 0(never writebasket = …again), - call
addAll(basket, newItems), - put
"sugar"at the start withunshift(), - print the basket and the count.
Expected output:
[ "sugar", "rice", "oil" ] Count: 3
Checklist
- Write
addAll(target, items)that adds every item to the end of target. - Empty the basket without assigning a new array.
- Refill it with
addAlland put sugar first withunshift(). - Print the basket and the count.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
