0
0
Javascriptprogramming~15 mins

Loop execution flow in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Loop Execution Flow
📖 Scenario: You are helping a small shop owner keep track of daily sales. Each day, the shop records the number of items sold. You want to calculate the total items sold over a week.
🎯 Goal: Build a program that uses a for loop to add up daily sales and show the total items sold in the week.
📋 What You'll Learn
Create an array with daily sales numbers for 7 days
Create a variable to hold the total sales starting at 0
Use a for loop to add each day's sales to the total
Print the total sales after the loop finishes
💡 Why This Matters
🌍 Real World
Shops and businesses often need to add up daily sales or counts to understand their performance over time.
💼 Career
Knowing how to use loops to process lists of data is a key skill for many programming jobs, especially in data processing and automation.
Progress0 / 4 steps
1
Create the daily sales data
Create an array called dailySales with these exact numbers: 5, 8, 6, 7, 4, 9, 3 representing sales for each day of the week.
Javascript
Need a hint?

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

2
Set up the total sales counter
Create a variable called totalSales and set it to 0 to start counting total items sold.
Javascript
Need a hint?

Use let to create a variable that can change later.

3
Add daily sales using a for loop
Use a for loop with a variable i starting at 0 and running while i < dailySales.length. Inside the loop, add dailySales[i] to totalSales.
Javascript
Need a hint?

Remember to use i++ to increase the counter each time.

4
Display the total sales
Write console.log(totalSales) to print the total items sold after the loop.
Javascript
Need a hint?

The total should be the sum of all numbers in the array.