Bird
Raised Fist0
Javascriptprogramming~15 mins

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

Choose your learning style10 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
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
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
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
Hint

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

Practice

(1/5)
1. Which of the following is the correct way to iterate over all elements in an array arr using for...of?
easy
A. arr.forEach(item => console.log(item));
B. for (let i = 0; i < arr.length; i++) { console.log(arr[i]); }
C. for (const i in arr) { console.log(arr[i]); }
D. for (const item of arr) { console.log(item); }

Solution

  1. Step 1: Understand for...of syntax

    The for...of loop directly accesses each element in the array, not the index.
  2. Step 2: Identify the correct loop structure

    for (const item of arr) { console.log(item); } uses for (const item of arr), which correctly iterates over elements.
  3. Final Answer:

    for (const item of arr) { console.log(item); } -> Option D
  4. Quick Check:

    for...of loops over elements = C [OK]
Hint: Use for...of to get elements directly [OK]
Common Mistakes:
  • Confusing for...in with for...of
  • Using index instead of element in for...of
  • Trying to use forEach syntax inside for...of
2. Which of the following is the correct syntax to use forEach to print each element of array nums?
easy
A. nums.forEach(function(item) { console.log(item); });
B. forEach(nums, item => console.log(item));
C. nums.forEach => console.log(item);
D. nums.forEach(item) { console.log(item); }

Solution

  1. Step 1: Recall forEach method syntax

    forEach is called on the array with a function as argument: array.forEach(function(item) { ... }).
  2. Step 2: Match correct syntax

    nums.forEach(function(item) { console.log(item); }); correctly uses nums.forEach(function(item) { console.log(item); }); which is valid.
  3. Final Answer:

    nums.forEach(function(item) { console.log(item); }); -> Option A
  4. Quick Check:

    array.forEach(function) syntax = A [OK]
Hint: Remember array.forEach(function) calls function on each item [OK]
Common Mistakes:
  • Trying to call forEach as a standalone function
  • Missing parentheses around function argument
  • Using arrow syntax incorrectly without parentheses
3. What will be the output of the following code?
const arr = [1, 2, 3];
let sum = 0;
for (const num of arr) {
  sum += num;
}
console.log(sum);
medium
A. 6
B. 123
C. 0
D. undefined

Solution

  1. Step 1: Understand the loop and sum calculation

    The loop goes through each number in arr: 1, 2, 3, adding each to sum.
  2. Step 2: Calculate the total sum

    sum = 0 + 1 + 2 + 3 = 6.
  3. Final Answer:

    6 -> Option A
  4. Quick Check:

    Sum of [1,2,3] = 6 [OK]
Hint: Add each element inside the loop to get total sum [OK]
Common Mistakes:
  • Concatenating numbers as strings instead of adding
  • Not initializing sum to 0
  • Using wrong loop variable
4. Find the error in this code snippet that tries to print each element of items:
const items = ['a', 'b', 'c'];
for (let i = 0; i <= items.length; i++) {
  console.log(items[i]);
}
medium
A. The console.log statement is missing parentheses
B. The variable i should be declared with const
C. The loop condition should be i < items.length instead of i <= items.length
D. The array items is not defined

Solution

  1. Step 1: Analyze the loop condition

    The loop runs while i <= items.length. Since array indices go from 0 to items.length - 1, this causes an out-of-bounds access at i = items.length.
  2. Step 2: Correct the loop condition

    Change condition to i < items.length to avoid accessing undefined element.
  3. Final Answer:

    The loop condition should be i < items.length instead of i <= items.length -> Option C
  4. Quick Check:

    Loop index must be less than length, not less or equal [OK]
Hint: Use i < array.length to avoid out-of-range errors [OK]
Common Mistakes:
  • Using <= instead of < in loop condition
  • Trying to declare loop variable with const
  • Ignoring array length boundaries
5. You have an array data = [0, 1, false, 2, '', 3]. You want to create a new array containing only the truthy values by iterating over data. Which code correctly does this?
hard
A. const result = data.filter(item => item == true);
B. const result = []; data.forEach(item => { if (item) result.push(item); });
C. const result = []; for (let i = 0; i < data.length; i++) { result.push(data[i]); }
D. const result = data.map(item => item ? item : null);

Solution

  1. Step 1: Understand truthy filtering

    We want to keep only values that are truthy (not 0, false, '', null, undefined).
  2. Step 2: Check each option

    const result = []; data.forEach(item => { if (item) result.push(item); }); uses forEach to push only truthy items, which is correct. const result = data.filter(item => item == true); uses == true which misses some truthy values like numbers. const result = []; for (let i = 0; i < data.length; i++) { result.push(data[i]); } copies all items without filtering. const result = data.map(item => item ? item : null); replaces falsy with null, not filtering.
  3. Final Answer:

    const result = []; data.forEach(item => { if (item) result.push(item); }); -> Option B
  4. Quick Check:

    Use forEach with if (item) to filter truthy [OK]
Hint: Use forEach with if (item) to keep truthy values [OK]
Common Mistakes:
  • Using == true instead of checking truthiness
  • Copying all elements without filtering
  • Using map instead of filter or manual push
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard