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:
| Day | Mon | Tue | Wed | Thu | Fri | Sat | Sun |
|---|---|---|---|---|---|---|---|
| Distance | 4500 | 5600 | 2400 | 3800 | 4200 | 6700 | 3600 |
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
For one day use the index: Monday is distanceList[0].
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600]; const stepCount = Math.round(distanceList[0] / 0.7); console.log(stepCount);
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:
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);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.
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);Longest: 6700 Shortest: 2400
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:
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]);Sat: 6700
Practice
From line 2: define the constant stepCount as Monday's distance ÷ 0.7, rounded with Math.round, then print it.
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
Press Run to see the result. Fill the blank to work out the steps for Saturday (distance 6700 ÷ 0.7, rounded with Math.round) without typing 6700 directly.
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600]; const stepCount = ___; console.log(stepCount);
Press Run to see the result. 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.
const distanceList = [4500, 5600, 2400, 3800, 4200, 6700, 3600];
___ (___) {
const stepCount = Math.round(distance / 0.7);
console.log(stepCount);
}Press Run to see the result. 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.
const scoreList = [40, 55, 67, 52, 120];
let sum = 0;
for (___) {
sum = sum + score;
}
console.log(sum);Press Run to see the result. 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.
const scoreList = [40, 55, 67, 52, 120];
let sum = 0;
for (const score of scoreList) {
sum = sum + score;
}
console.log(___);Press Run to see the result. Temperatures in St Catherine over a winter week. Find the lowest one with a loop (without Math.min) and print Coldest: -4.
Hint
Startmin at temps[0].const temps = [3, -1, 2, -4, 0, 1, 5];
Press Run to see the result. In the two arrays, student number i is names[i] with mark marks[i]. Find the top student and print Top: Salma with 97.
Hint
Keep the indexbest, not the mark itself, then use it with both arrays.const names = ["Omar", "Nour", "Salma", "Karim", "Hana"]; const marks = [88, 91, 97, 79, 94];
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.