0
0
Javascriptprogramming~15 mins

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

Choose your learning style9 modes available
Iterating over arrays
πŸ“– Scenario: You are helping a small shop owner who keeps a list of daily sales amounts. They want to find out the total sales for the day by adding all the amounts together.
🎯 Goal: Build a simple JavaScript program that stores daily sales in an array, then uses a loop to add all sales amounts and finally prints the total sales.
πŸ“‹ What You'll Learn
Create an array called dailySales with the exact values: 100, 200, 150, 50, 300
Create a variable called totalSales and set it to 0
Use a for loop with the variable sale to iterate over dailySales
Add each sale to totalSales inside the loop
Print the value of totalSales using console.log
πŸ’‘ Why This Matters
🌍 Real World
Adding up daily sales is a common task in shops and businesses to track earnings.
πŸ’Ό Career
Knowing how to loop through arrays and sum values is a basic skill for many programming jobs, especially in data processing and web development.
Progress0 / 4 steps
1
Create the sales array
Create an array called dailySales with these exact numbers: 100, 200, 150, 50, 300
Javascript
Need a hint?

Use square brackets [] to create an array and separate numbers with commas.

2
Create the totalSales variable
Create a variable called totalSales and set it to 0
Javascript
Need a hint?

Use let to create a variable that can change later.

3
Add sales using a for loop
Use a for loop with the variable sale to go through each number in dailySales. Inside the loop, add each sale to totalSales
Javascript
Need a hint?

Use for (const sale of dailySales) to loop through the array.

4
Print the total sales
Print the value of totalSales using console.log
Javascript
Need a hint?

Use console.log(totalSales); to show the total in the console.