PatrickFrankAIU / ITWEB220-2404A

Learning resources for students in ITWEB 220, term 2404A.
0 stars 0 forks source link

Fully Worked Problem: High Scores and Counting Threshold #22

Open PatrickFrankAIU opened 3 months ago

PatrickFrankAIU commented 3 months ago

This is a "fully worked" problem, meaning both the instructions and the solution are provided. This problem was fully worked during class session "Module 2 Instructor Live 3", on August 19th, and that recording is available in the classroom announcements. A good idea here is to try to work the problem again later on your own, without looking at the solution. I've also attached at the end of this Issue some suggested upgrades that you can try, all of which are completable with your current level of knowledge.

Instructions: Imagine you have an array of numbers representing scores from a game. You want to count how many scores are above a certain threshold. For those special high scores, you also want to keep a separate count, all the while using a while loop for iteration.

let scores = [15, 22, 18, 34, 45, 5, 10, 25]; // Example scores in an array
let highScoreThreshold = 20; // Scores above this value are considered high
let totalScoresCount = 0; // Counter for all scores processed
let highScoresCount = 0; // Counter for high scores

Tips:

=================

Solution:

let scores = [15, 22, 18, 34, 45, 5, 10, 25]; // Example scores
let highScoreThreshold = 20; // Scores above this value are considered high
let totalScoresCount = 0; // Counter for all scores processed
let highScoresCount = 0; // Counter for high scores

let i = 0; // Initialize loop counter

while (i < scores.length) {
    let score = scores[i];

    // Count all scores
    totalScoresCount++;

    // Increment highScoresCount if the score is above the threshold
    if (score > highScoreThreshold) {
        highScoresCount++;
    }

    i++; // Increment the loop counter
}

console.log("Total scores processed: " + totalScoresCount);
console.log("Number of high scores: " + highScoresCount);

Additional Practice Suggestions: