Back

Activity 4.4 — Processing Arrays

divider

Introduction

Activity 4.4

Processing Data with Arrays

Topics

  • Processing arrays using traversal
  • Counting elements
  • Calculating totals and averages
  • Using conditionals with processed data

From Lists to Data

In the previous activity, arrays helped us store and display information.

In this activity, arrays become data — something we can analyze, summarize, and make decisions with.

The Processing Pattern

Processing an array usually follows this pattern:

  • Start with an accumulator variable (count, total)
  • Traverse the array
  • Update the accumulator each loop
  • Use the final result

Demo: Counting Items

// Processing Pattern: Counting Elements
let tasks = ["Homework", "Study", "Workout"];
let count = 0;
let i = 0;
while (i < tasks.length) {
count++;
i++;
}
console.log("Number of tasks:", count);

Demo: Total & Average

// Processing Pattern: Total + Average
let scores = [85, 90, 78, 92, 88];
let total = 0;
let i = 0;
while (i < scores.length) {
total += scores[i];
i++;
}
let average = total / scores.length;
console.log("Average score:", average);

Why This Matters

  • This is how programs analyze survey results
  • This is how grades are summarized
  • This pattern appears directly in the Create Performance Task

'F' → Fullscreen

Objectives

  • icon Use traversal to process every element in an array.
  • icon Count elements using an accumulator variable.
  • icon Calculate totals and averages from numeric arrays.
  • icon Use conditionals based on processed results.
divider

Activity Tasks

  • icon Create a new project named arrays-processing.
  • icon Complete each task individually.

Task 1: Task Counter

  • icon Use traversal to count how many tasks exist.
  • icon Store the result in a variable named count.
Task 1 — Task Counter
console.log("--- Demo 1: Task Counter ---");
// Starter task list
let tasks = [
"Finish math homework",
"Practice arrays",
"Pack gym clothes",
"Email coach"
];
// Use traversal to COUNT how many tasks exist
let count = 0;
let i = 0;
while (i < tasks.length) {
count++;
i++;
}
// Print the result
console.log("You have " + count + " tasks to complete.");
prompt("Press enter to continue...");

Task 2: Score Analyzer

  • icon Calculate the total and average of all scores.
  • icon Use an if/else statement with the average.
Task 2 — Score Analyzer
console.log("--- Demo 2: Score Analyzer ---");
// Starter data
let scores = [72, 85, 90, 66, 88];
// Step 1: Calculate the TOTAL of all scores
let total = 0;
let i = 0;
while (i < scores.length) {
total += scores[i];
i++;
}
// Step 2: Calculate the AVERAGE score
let average = total / scores.length;
// Step 3: Print results
console.log("Total score:", total);
console.log("Average score:", average);
// Step 4: Print a message based on the average
if (average >= 70) {
console.log("Class average is passing.");
} else {
console.log("Class average is not passing.");
}
prompt("Press enter to continue...");
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
--- Demo 1: Task Counter ---
You have 4 tasks to complete.
Press enter to continue... [Enter]
--- Demo 2: Score Analyzer ---
Total score: 401
Average score: 80.2
Class average is passing.
Press enter to continue... [Enter]
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. What is the purpose of an accumulator variable?
  2. Why do we divide by array.length when calculating an average?
  3. How does traversal allow a program to analyze data?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete