Learn
Maps are also easy to add key-value pairs or to change the value of an existing pair. In both cases we specify the map name, the key, and the value:
yourMap[newKey] = newValue
So to add a new customer balance, we could do:
customers["Samantha"] = 1.25
We can use the same format to change an existing value. The format is the same! So when Samantha buys another donut:
customers["Samantha"] = 2.75
We now have the ability to add and update values in our maps. In the next exercise, we’ll learn how to remove key-value pairs. Before then let’s practice adding and updating!
Instructions
1.
Imagine our baker bringing out a tray of 12 fresh glazed
donuts. Let’s add 12
donuts under the key glazed
into our map.
After we add, print out the value of the glazed
key.
2.
Next, update count of the jelly
key to be 3
.
Then print out the map.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.