JAVASCRIPT · ОСНОВЫ · LESSON 20

Bottom line: counting points

Array, function and condition · about 18 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Put it together.

The function can run through the array and collect the amount into a variable.

function total(scores) {
  let sum = 0;
  for (const score of scores) {
    sum += score;
  }
  return sum;
}
Outcome programme
MassiveglassesFunctionsumifcheck-up

✦ The method returns the result.

STEP 2

Check the result.

Create an array of points, call the function and compare the amount with the desired border.

const scores = [2, 3, 4];
const result = total(scores);
if (result >= 8) {
  console.log("Победа");
}

✦ The sum is 9, so there will be a victory.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the names, brackets and signs.

function total(scores) {
  let sum = 0;
  for (const score of scores) {
    sum += score;
  }
  return sum;
}
const scores = [2, 3, 4];
const result = total(scores);
if (result >= 8) {
  console.log("Победа");
}
Open the editor for class work
HOMEWORK

Solve it yourself

Write the function total(Zscores): fold the points with the loop for...of and return the amount. For the array [1, 2, 5] output «Готово» if the result is not less than 8.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework