 Filtering and Counting with Loops
Filtering 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.
12345678910const 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);
12345678910const 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);
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.
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 7.69 Filtering and Counting with Loops
Filtering and Counting with Loops
Sveip for å vise menyen
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.
12345678910const 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);
12345678910const 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);
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.
Takk for tilbakemeldingene dine!