0
0
Javascriptprogramming~15 mins

For loop in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a For Loop to Sum Numbers
📖 Scenario: You are helping a small shop owner who wants to add up the prices of some items they sold today.
🎯 Goal: Build a simple program that uses a for loop to add all the prices in a list and show the total amount earned.
📋 What You'll Learn
Create an array called prices with the exact values: 10, 20, 30, 40, 50
Create a variable called total and set it to 0
Use a for loop with the variable i to go through each item in prices
Add each price to the total inside the loop
Print the total after the loop finishes
💡 Why This Matters
🌍 Real World
Adding up prices is a common task in shopping apps, cash registers, and budgeting tools.
💼 Career
Understanding loops and arrays is essential for many programming jobs, especially in web development and data processing.
Progress0 / 4 steps
1
Create the prices array
Create an array called prices with these exact numbers: 10, 20, 30, 40, 50.
Javascript
Need a hint?

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

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

Use let to create a variable that can change later.

3
Use a for loop to add prices
Use a for loop with the variable i to go through each item in prices. Inside the loop, add prices[i] to total.
Javascript
Need a hint?

Start i at 0 and loop while it is less than prices.length. Add each price to total inside the loop.

4
Print the total
Write console.log(total) to print the total sum after the loop.
Javascript
Need a hint?

Use console.log(total) to show the result in the console.