Home > Software design >  Why does the do-while loop run when the test grades are between 0 - 100?
Why does the do-while loop run when the test grades are between 0 - 100?

Time:11-06

        let num1 = prompt("Enter Eaxam 1");
        let num2 = prompt("Enter Eaxam 2");
        let num3 = prompt("Enter Eaxam 3");
        let num4 = prompt("Enter Eaxam 4");
        let num5 = prompt("Enter Eaxam 5");

        var grade1 = parseInt(num1);
        var grade2 = parseInt(num2);  
        var grade3 = parseInt(num3);  
        var grade4 = parseInt(num4);  
        var grade5 = parseInt(num5);    

        let averageGrade = ((grade1 grade2 grade3 grade4 grade5)/5);
        console.log(averageGrade);

        do{
            console.log("Please enter a valid test grade!")
            let num1 = prompt("Enter Eaxam 1");
            let num2 = prompt("Enter Eaxam 2");
            let num3 = prompt("Enter Eaxam 3");
            let num4 = prompt("Enter Eaxam 4");
            let num5 = prompt("Enter Eaxam 5");

            var grade1 = parseInt(num1);
            var grade2 = parseInt(num2);  
            var grade3 = parseInt(num3);  
            var grade4 = parseInt(num4);  
            var grade5 = parseInt(num5)
        }
        while((num1 > 100 || num1 < 0) || (num2 > 100 || num2 < 0) ||  (num3 > 100 || num3 < 0) ||  (num4 > 100 || num4 < 0) ||  (num5 > 100 || num5 < 0))

        switch(true){
            case (averageGrade >= 90):
                console.log("You recived an A")
                break;
            case (averageGrade <= 89 && averageGrade >= 80):
                console.log("You recived a B")
                break;
            case (averageGrade <= 79 && averageGrade >= 70):
                console.log("You recived a C")
                break;
            case (averageGrade <= 69 && averageGrade >= 60):
                console.log("You recived a D")
                break;
            case (averageGrade <= 59):
                console.log("You failed dumbass")
                break;
        }

I tried adding a block of code that would validate the grades to make sure they are between 0 to 100 but for some reason the whole do-while loop executes despite the grades going between 0-100. What are some things that are wrong with my code? Thanks.

CodePudding user response:

Understand that in a do...while loop, the loop always executes at least once. so you either need to omit the duplicate code outside of the loop, or use a while loop instead.

Here is a solution that lets you keep the console.log message if the user enters an invalid value for the first set of prompts:

let grade1;
let grade2;
let grade3;
let grade4;
let grade5;

const promtGrades = () => {
  grade1 = parseInt(prompt('Enter Eaxam 1'));
  grade2 = parseInt(prompt('Enter Eaxam 2'));
  grade3 = parseInt(prompt('Enter Eaxam 3'));
  grade4 = parseInt(prompt('Enter Eaxam 4'));
  grade5 = parseInt(prompt('Enter Eaxam 5'));
};

promtGrades();

while (
  [grade1, grade2, grade3, grade4, grade5].some(
    (grade) => grade < 0 || grade > 100
  )
) {
  console.log('Please enter a valid test grade!');
  promtGrades();
}

let averageGrade = (grade1   grade2   grade3   grade4   grade5) / 5;
console.log(averageGrade);

switch (true) {
  case averageGrade >= 90:
    console.log('You recived an A');
    break;
  case averageGrade <= 89 && averageGrade >= 80:
    console.log('You recived a B');
    break;
  case averageGrade <= 79 && averageGrade >= 70:
    console.log('You recived a C');
    break;
  case averageGrade <= 69 && averageGrade >= 60:
    console.log('You recived a D');
    break;
  case averageGrade <= 59:
    console.log('You failed dumbass');
    break;
}

Hope this helps.

CodePudding user response:

A do...while loop will run at least once and repeat itself if the condition provided evaluated to true, and stop once it evaluates to false. You probably want a while loop or only declare the variables outside the do...while loop and prompt the user then assign said values to the variables.

let num1, num2, num3, num4, num5, grade1, grade2, grade3, grade4, grade5;

do {
    num1 = prompt("Enter Eaxam 1");
    num2 = prompt("Enter Eaxam 2");
    num3 = prompt("Enter Eaxam 3");
    num4 = prompt("Enter Eaxam 4");
    num5 = prompt("Enter Eaxam 5");

    grade1 = parseInt(num1);
    grade2 = parseInt(num2);  
    grade3 = parseInt(num3);  
    grade4 = parseInt(num4);  
    grade5 = parseInt(num5)
} while((num1 > 100 || num1 < 0) || (num2 > 100 || num2 < 0) ||  (num3 > 100 || num3 < 0) ||  (num4 > 100 || num4 < 0) ||  (num5 > 100 || num5 < 0))

const averageGrade = ((grade1 grade2 grade3 grade4 grade5)/5);
console.log(averageGrade);

Side Note: You should probably learn about arrays so you can reduce the number of variables you need and make your code more dynamic in that it will be able to expand to any number of exams instead of the hard coded 5. You should also learn about scopes instead of using var and stick with let and const. To reassign a variable you do not need to use the keyword let again. You're also not using a switch statement correctly.

const numOfExams = 5
const grades = []
for (let i = 0; i < numOfExams;   i) {
    let grade
    do {
        grade = parseInt(prompt(`Enter Exam ${i}`))
    } while (grade < 0 || 100 < grade || `${grade}` === 'NaN')
    grades.push(grade)
}

const averageGrade = grades.reduce((sum, grade) => sum   grade, 0) / grades.length

const mark = [
    ['A', 90],
    ['B', 80],
    ['C', 70],
    ['D', 60]
].find(array => array[1] <= averageGrade)
if (mark)
    console.log(`You received an ${mark[0]}`)
else
    console.log('You failed dumbass')
  • Related