0
0
Javascriptprogramming~15 mins

Iterating over objects in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Iterating over objects
๐Ÿ“– Scenario: You work in a small shop and have a list of products with their prices stored in an object. You want to check and display each product's price.
๐ŸŽฏ Goal: Build a simple program that iterates over an object of products and their prices, then prints each product name with its price.
๐Ÿ“‹ What You'll Learn
Create an object called products with exact product-price pairs
Create a variable called currency to store the currency symbol
Use a for...in loop with variable product to iterate over products
Print each product and its price with the currency symbol
๐Ÿ’ก Why This Matters
๐ŸŒ Real World
Shop owners and cashiers often need to list products and prices from a database or object to show customers.
๐Ÿ’ผ Career
Knowing how to iterate over objects is essential for web developers working with data structures and displaying information dynamically.
Progress0 / 4 steps
1
Create the products object
Create an object called products with these exact entries: "apple": 1.2, "banana": 0.5, "orange": 0.8
Javascript
Need a hint?

Use const products = {} and add the exact key-value pairs inside curly braces.

2
Add a currency symbol variable
Create a variable called currency and set it to the string "$"
Javascript
Need a hint?

Use const currency = "$"; to store the currency symbol.

3
Iterate over the products object
Use a for...in loop with variable product to iterate over products and create a variable price to get the price for each product
Javascript
Need a hint?

Use for (const product in products) and inside the loop get the price with products[product].

4
Print each product and its price
Inside the for...in loop, write a console.log statement to print the product name and its price with the currency symbol, formatted like: apple: $1.2
Javascript
Need a hint?

Use console.log(`${product}: ${currency}${price}`); inside the loop to print each line.