Back

Activity 4.2 — Using .length

divider

Introduction

Data Structures with Arrays

Lesson 2 — Access + Update + .length

Topics

  • Using .length to measure an array
  • Finding the last index with length - 1
  • Accessing first and last elements safely
  • Updating elements using index expressions
  • Using array data inside if/else

'F' → Fullscreen

Objectives

  • icon Use .length to determine how many elements are in an array.
  • icon Compute the last index using array.length - 1.
  • icon Access and update the first and last elements without hard-coding the last index.
  • icon Use an if/else statement based on array data.
divider

Activity Tasks

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

Task 1: Class Score Snapshot

  • icon Create an array, print .length, and safely display the first and last values.
  • icon Do not hard-code the last index; use lastIndex = scores.length - 1.
Task 1 — Snapshot
// Task 1 — "Class Score Snapshot" (Create + Access + .length)
//
// Goal: Use .length to safely access the last element and print a short report.
// Step 1: Create an array named 'scores' with at least 6 quiz scores.
let scores = [84, 91, 76, 88, 95, 82];
// Step 2: Print how many scores are stored.
console.log("Number of scores:", scores.length);
// Step 3: Create a variable named 'lastIndex' equal to the last valid index.
let lastIndex = scores.length - 1;
// Step 4: Print the first and last score (do NOT hard-code the last index).
console.log("First score:", scores[0]);
console.log("Last score:", scores[lastIndex]);
// Step 5: Print ONE summary line exactly like this format:
// We have X scores. First: A, Last: B
console.log("We have " + scores.length + " scores. First: " + scores[0] + ", Last: " + scores[lastIndex]);

Task 2: Score Fix + Pass Check

  • icon Update the first and last elements safely, then print the updated array.
  • icon Use if/else to print whether the last score is passing.
Task 2 — Update + Decision
// Task 2 — "Score Fix + Pass Check" (Update + Decision)
//
// Scenario: The first and last scores were entered incorrectly.
// - The first score should be 90
// - The last score should be 87
//
// Step 1: Update the FIRST score to 90.
scores[0] = 90;
// Step 2: Update the LAST score to 87 using lastIndex (do NOT hard-code).
scores[lastIndex] = 87;
// Step 3: Print the updated scores array.
console.log("Updated scores:", scores);
// Step 4: Print whether the LAST score is passing (70 or higher).
// Print exactly ONE of these lines:
// "Last score is passing."
// "Last score is not passing."
if (scores[lastIndex] >= 70) {
console.log("Last score is passing.");
} else {
console.log("Last score is not passing.");
}
// Step 5 (Optional Challenge):
// Print: Last index is X (array length is Y).
// Use lastIndex and scores.length
divider

Sample Output

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

Sample Output
Number of scores: 6
First score: 84
Last score: 82
We have 6 scores. First: 84, Last: 82
Updated scores: [90, 91, 76, 88, 95, 87]
Last score is passing.
divider

Reflection Questions

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

  1. Why is array.length - 1 the last valid index?
  2. What is wrong with scores[scores.length]?
  3. Why is it better to use lastIndex instead of hard-coding a number?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete