Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Filtering and Counting with Loops | Applying Logic and Writing Clean Code
Conditional Statements and Loops in JavaScript

bookFiltering and Counting with Loops

Combining loops and conditions is a powerful way to process data in JavaScript. Imagine you have a list of student exam scores, and you want to know how many students passed. You can use a loop to go through each score, and a condition to check if it meets the passing standard. For instance, if the passing score is 60, you would count every score that is 60 or higher.

12345678910
const scores = [75, 42, 89, 55, 63, 48, 91, 60]; let passCount = 0; for (let i = 0; i < scores.length; i++) { if (scores[i] >= 60) { passCount++; } } console.log("Number of students who passed: " + passCount);
copy
12345678910
const scores = [75, 42, 89, 55, 63, 48, 91, 60]; let passCount = 0; scores.forEach(function(score) { if (score >= 60) { passCount++; } }); console.log("Number of students who passed: " + passCount);
copy

1. Where should you place the condition to check if a score is passing when counting inside a loop?

2. Fill in the blank with the correct condition to count how many numbers in the array are even.

question mark

Where should you place the condition to check if a score is passing when counting inside a loop?

Select the correct answer

question-icon

Fill in the blank with the correct condition to count how many numbers in the array are even.

const numbers = [2, 5, 8, 13, 21, 34, 55, 89]; let evenCount = 0; for (let i = 0; i < numbers.length; i++) { if () { evenCount++; } } console.log("Number of even numbers: " + evenCount);
Number of even numbers: 3

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Awesome!

Completion rate improved to 7.69

bookFiltering and Counting with Loops

Glissez pour afficher le menu

Combining loops and conditions is a powerful way to process data in JavaScript. Imagine you have a list of student exam scores, and you want to know how many students passed. You can use a loop to go through each score, and a condition to check if it meets the passing standard. For instance, if the passing score is 60, you would count every score that is 60 or higher.

12345678910
const scores = [75, 42, 89, 55, 63, 48, 91, 60]; let passCount = 0; for (let i = 0; i < scores.length; i++) { if (scores[i] >= 60) { passCount++; } } console.log("Number of students who passed: " + passCount);
copy
12345678910
const scores = [75, 42, 89, 55, 63, 48, 91, 60]; let passCount = 0; scores.forEach(function(score) { if (score >= 60) { passCount++; } }); console.log("Number of students who passed: " + passCount);
copy

1. Where should you place the condition to check if a score is passing when counting inside a loop?

2. Fill in the blank with the correct condition to count how many numbers in the array are even.

question mark

Where should you place the condition to check if a score is passing when counting inside a loop?

Select the correct answer

question-icon

Fill in the blank with the correct condition to count how many numbers in the array are even.

const numbers = [2, 5, 8, 13, 21, 34, 55, 89]; let evenCount = 0; for (let i = 0; i < numbers.length; i++) { if () { evenCount++; } } console.log("Number of even numbers: " + evenCount);
Number of even numbers: 3

Click or drag`n`drop items and fill in the blanks

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 1
some-alt