0

I have tried looking at documentation and could not figure it out. My while loop does not stop and prints "ya" forever. What am I doing wrong?

var ya = 1;
Formitivegrades = [100, 95, 100, 85, 100]
Formitiaveav1 = Formitivegrades[0] + Formitivegrades[1] + Formitivegrades[3] + Formitivegrades[4] + Formitivegrades[2]
Formitiaveav2 = Formitiaveav1 / 5

sumgrades = [100]
sumav1 = ya + sumgrades[0]
sumav2 = sumav1 / 2

averge = .4 * Formitiaveav2 + .6 * sumav2

while(averge < 95){
    console.log(ya)
    ya++;
}

console.log(averge)
3
  • 1
    you need to recalculate the average in the while loop Commented Aug 22, 2021 at 1:20
  • 2
    averge never changes. So the loop will always be true Commented Aug 22, 2021 at 1:20
  • averge will always be 68.7 and It is not changing, so the loop runs forever. Commented Aug 22, 2021 at 1:23

1 Answer 1

1

Don't think of the equal sign = as an equality that is guaranteed forever, the way it is in mathematics. In programming it is a one-time assignment. An assignment statement gives a variable a new value at that moment in time only. If they change values afterwards the equality can be broken.

If ya is updated that does not cause sumav1 to be recalculated. Nor does sumav2 change whenever sumav1 changes, nor avrge when sumav2 changes. averge keeps the same value forever unless you explicitly update it.

To that end, you need to recompute all of the variables each time you change ya. That way averge can eventually reach 95 and cause the loop to end.

while(averge < 95){
    console.log(ya)
    ya++;

    sumav1 = ya + sumgrades[0]
    sumav2 = sumav1 / 2
    averge = .4 * Formitiaveav2 + .6 * sumav2
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.