Skip to content
Advertisement

JavaScript is displaying an incorrect mathematical answer

I wrote this small script that would calculate a students GPA. I decided to test it midway and it appears as if the code messes up.

JavaScript

For example if I add 2 and 2 and 12 instead of displaying 2 it displays 1.75 which is weird.

Advertisement

Answer

You forget to add the third values so you get wrong results.
If you inputs 2,2,12 result is 0.5, because of (2+2)/8 = 4/8 = 0.5.
For 2,12,2 or 12,2,2 result is 1,75 because of (2+12)/8 = 14/8 = 1,75

This should give you the desired result.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement