JavaScript / Arrays

Arrays: index, for...of and length together

Combine the index, for...of and length in one program, and find the largest and smallest values yourself. Baccalaureate

Let's build a step counter. Steps = distance in metres ÷ step length (0.7 m), rounded with Math.round because a step count is a whole number. We store a week of distances in an array, starting on Monday:

DayMonTueWedThuFriSatSun
Distance4500560024003800420067003600
Index0123456

For one day use the index: Monday is distanceList[0].

JavaScript
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
const stepCount = Math.round(distanceList[0] / 0.7);
console.log(stepCount);
Console
6429

For every day use for...of and write distance instead of distanceList[0]. Then, for the average, add the distances up and divide by length:

JavaScript
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
let sum = 0;
for (const distance of distanceList) {
  sum = sum + distance;
}
console.log(sum);
console.log(sum / distanceList.length);
Console
30800
4400
sum uses let because its value changes. If you type 7 instead of distanceList.length, the average becomes wrong as soon as you add more days.

Largest and smallest value

How do we find the longest distance? Assume the first value is the largest, then go through every value: whenever one is bigger than what we are holding, keep it instead. At the end we are holding the largest.

JavaScript
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
let max = distanceList[0];
let min = distanceList[0];
for (const distance of distanceList) {
  if (distance > max) {
    max = distance;
  }
  if (distance < min) {
    min = distance;
  }
}
console.log("Longest: " + max);
console.log("Shortest: " + min);
Console
Longest: 6700
Shortest: 2400
Start max and min with the first value, not 0. If min started at 0, no value would be smaller, and the answer would be 0, which is not even in the array.

To know which day had the longest distance we need the index, so we go back to a counter loop and keep i:

JavaScript
const days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
let best = 0;
for (let i = 1; i < distanceList.length; i++) {
  if (distanceList[i] > distanceList[best]) {
    best = i;
  }
}
console.log(days[best] + ": " + distanceList[best]);
Console
Sat: 6700

Practice

1Write the programCurriculum practice

From line 2: define the constant stepCount as Monday's distance ÷ 0.7, rounded with Math.round, then print it.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
Console
Press Run to see the result.
2Fill in the blankCurriculum practice

Fill the blank to work out the steps for Saturday (distance 6700 ÷ 0.7, rounded with Math.round) without typing 6700 directly.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
const stepCount = ___;
console.log(stepCount);
Console
Press Run to see the result.
3Fill in the blankCurriculum practice

Fill both blanks: use for...of to take the values of distanceList into the constant distance, so the program prints the steps for each day.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];

___ (___) {
  const stepCount = Math.round(distance / 0.7);
  console.log(stepCount);
}
Console
Press Run to see the result.
4Fill in the blankCurriculum practice

Fill the blank with a for...of loop that takes the values of scoreList into the constant score, so the program prints the total score.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const scoreList = [40, 55, 67, 52, 120];
let sum = 0;

for (___) {
  sum = sum + score;
}
console.log(sum);
Console
Press Run to see the result.
5Fill in the blankCurriculum practice

Fill the blank to print the average: the total ÷ the number of values in scoreList. Write it so it stays correct if the number of values changes.

JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const scoreList = [40, 55, 67, 52, 120];
let sum = 0;
for (const score of scoreList) {
  sum = sum + score;
}
console.log(___);
Console
Press Run to see the result.
6Write the programExtra practice

Temperatures in St Catherine over a winter week. Find the lowest one with a loop (without Math.min) and print Coldest: -4.

HintStart min at temps[0].
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const temps = [3, -1, 2, -4, 0, 1, 5];
Console
Press Run to see the result.
7Write the programExtra practice

In the two arrays, student number i is names[i] with mark marks[i]. Find the top student and print Top: Salma with 97.

HintKeep the index best, not the mark itself, then use it with both arrays.
JavaScript Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
const names = ["Omar", "Nour", "Salma", "Karim", "Hana"];
const marks = [88, 91, 97, 79, 94];
Console
Press Run to see the result.

Key points

  • One value: list[i]. Every value: for (const x of list). Average: sum / list.length.
  • For the largest value, start with list[0] and replace it with any bigger value inside the loop; the smallest works the same with <.
  • If you need where a value is, use a counter loop and keep i.