Back

Activity 4.1 — Data Structures with Array

divider

Introduction

Activity 4.1

Data Structures with Arrays

Why Arrays Exist

  • Storing 10 related values as separate variables is annoying.
  • Storing 30+ values becomes hard to manage and easy to mess up.
  • Arrays store many related values under one variable name.

Vocabulary

  • Array: a collection of values stored together
  • Element: one value inside the array
  • Index: the position number of an element
  • Zero-based indexing: first element is at index 0

Today's Skills

  • Create an array using [ ]
  • Access elements using array[index]
  • Update an element with assignment

'F' → Fullscreen

Objectives

  • icon Create arrays that store multiple related values.
  • icon Access elements using indexes (starting at 0).
  • icon Modify array elements by assigning a new value.
divider

Activity Tasks

  • icon Create a new project named 4-1-arrays.js.
  • icon Complete each task individually.
  • icon If you get stuck, use the figures as examples and ask for help early.

Figures: Examples

Figure 1 — Create + Access
// Arrays store a collection of related values under one variable name.
let scores = [84, 91, 76, 88, 95];
// Elements are accessed by index (position).
// Indexes start at 0.
console.log(scores[0]); // first element
console.log(scores[2]); // third element
Figure 2 — Modify an Element
// Updating an element
let scores = [84, 91, 76, 88, 95];
// Change the second score (index 1) to 100
scores[1] = 100;
console.log(scores);

Task 1: Create + Access

  • icon Create an array of strings and print specific elements.
  • icon Answer the last-index question as a comment.
Task 1 — Create + Access
// Task 1 — Create and Access Arrays
// Follow the steps below.
// 1) Create an array named 'foods' with 5 food strings.
let foods = ["pizza", "tacos", "sushi", "pasta", "burgers"];
// 2) Print the FIRST food (index 0).
console.log("First food:", foods[0]);
// 3) Print the THIRD food (index 2).
console.log("Third food:", foods[2]);
// 4) Print the LAST food.
// (Hint: for today, you may use the last index directly since you know there are 5 items.)
console.log("Last food:", foods[4]);
// 5) Answer (in a comment):
// If foods has 5 elements, what is the last index? Why?

Task 2: Modify + Mini Challenge

  • icon Update elements by index, then print the updated array.
  • icon Mini challenge: compute a range using Math.max and Math.min.
Task 2 — Modify + Mini Challenge
// Task 2 — Modify an Array + Mini Challenge
let temps = [72, 68, 75, 70, 69];
// 1) Print the temperature at index 3 with a label.
console.log("Temp at index 3:", temps[3]);
// 2) Update the FIRST temperature to 74.
temps[0] = 74;
// 3) Update the LAST temperature to 71.
temps[4] = 71;
// 4) Print the entire temps array so we can see the changes.
console.log("Updated temps:", temps);
// 5) Mini Challenge (no loops):
// Create a variable named 'range' that equals highest - lowest.
// Use ONLY index access (temps[?]) and Math.max/Math.min.
// Then print: "Range: X"
//
// Hint: Math.max(temps[0], temps[1], temps[2], temps[3], temps[4])
// Hint: Math.min(temps[0], temps[1], temps[2], temps[3], temps[4])
divider

Sample Output

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

Sample Output
First food: pizza
Third food: sushi
Last food: burgers
Temp at index 3: 70
Updated temps: [74, 68, 75, 70, 71]
Range: 7
divider

Reflection Questions

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

  1. What is an index? What is an element?
  2. Why do arrays start at index 0 (zero-based indexing)?
  3. Explain one reason arrays are easier to manage than 30 separate variables.
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete