Back

Activity 4.5 — Survey Analyzer

divider

Introduction

Activity 4.5

Analyzing Real-World Data

Topics

  • Collecting user input
  • Storing survey data in arrays
  • Processing data with traversal
  • Summarizing results

Why Analyze Data?

Many real programs don’t just store information — they analyze it.

Surveys, fitness apps, and recommendation systems all rely on processing collections of data.

The Survey Question

In this activity, your program analyzes responses to the question:

“How many hours of sleep did you get last night?”

Processing Pattern Review

Most data-processing programs follow this pattern:

  • Collect values into a list
  • Traverse the list
  • Update one or more accumulator variables
  • Use the results to produce output

Demo: Calculating an Average

// Survey data example
let responses = [6, 7, 5, 8, 6];
// Processing pattern
let total = 0;
let i = 0;
while (i < responses.length) {
total += responses[i];
i++;
}
let average = total / responses.length;
console.log("Average:", average);

Demo: Counting Below a Threshold

// Counting values that meet a condition
let belowSeven = 0;
let i = 0;
while (i < responses.length) {
if (responses[i] < 7) {
belowSeven++;
}
i++;
}
console.log("Below 7 hours:", belowSeven);

Why This Matters

  • This pattern appears directly in the Create Performance Task
  • Simple programs can still analyze meaningful data
  • Clear logic is more important than complexity

'F' → Fullscreen

Objectives

  • icon Collect user input and store it in an array.
  • icon Use traversal to process survey data.
  • icon Calculate averages from numeric lists.
  • icon Count values that meet a condition.
divider

Activity Tasks

  • icon Create a new project named survey-analyzer.
  • icon Complete each task individually.

Task 1: Collect Survey Data

  • icon Use prompt() to collect responses.
  • icon Store each response using push().
  • icon Stop collecting data when the user types done.
Task 1 — Collect Data
console.log("--- Demo 1: Collect Survey Data ---");
let responses = [];
let input = "";
while (input != "done") {
input = prompt("Enter hours of sleep (or type 'done'):");
if (input != "done") {
responses.push(parseInt(input));
}
}
console.log("Responses collected:", responses.length);
prompt("Press enter to continue...");

Task 2: Analyze Results

  • icon Calculate the average hours of sleep.
  • icon Count how many responses are below 7 hours.
  • icon Print a clear summary of the results.
Task 2 — Survey Analyzer
console.log("--- Demo 2: Survey Analyzer ---");
// Sample data (use your collected data here)
let responses = [6, 7, 5, 8, 6, 4, 9];
// Step 1: Calculate total and average
let total = 0;
let i = 0;
while (i < responses.length) {
total += responses[i];
i++;
}
let average = total / responses.length;
// Step 2: Count responses below 7 hours
let belowSeven = 0;
i = 0;
while (i < responses.length) {
if (responses[i] < 7) {
belowSeven++;
}
i++;
}
// Step 3: Output results
console.log("Number of responses:", responses.length);
console.log("Average hours of sleep:", average);
console.log("Students below 7 hours:", belowSeven);
prompt("Press enter to continue...");
divider

Sample Output

Sample Output
--- Demo 1: Collect Survey Data ---
Enter hours of sleep (or type 'done'): 6
Enter hours of sleep (or type 'done'): 7
Enter hours of sleep (or type 'done'): 5
Enter hours of sleep (or type 'done'): 8
Enter hours of sleep (or type 'done'): done
Responses collected: 4
Press enter to continue... [Enter]
--- Demo 2: Survey Analyzer ---
Number of responses: 7
Average hours of sleep: 6.43
Students below 7 hours: 4
Press enter to continue... [Enter]
divider

Reflection Questions

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

  1. Why is traversal necessary to calculate an average?
  2. What role do accumulator variables play in data processing?
  3. How does this program demonstrate meaningful data analysis?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete