averaging numbers and display grouped average - javascript

I am trying to write a code that prompts the user to add 4 numbers and average them out and depending if that answer is 100+, 75-100, 50-75, 50- it checks if its in between the specified number zones and shows the answer respectively in a prompt box.
edit The code doesn't give anything lower than group 75-100 regardless. for example, if I enter four 20's it says that its between 75 and 100 (a+ grade) which makes no sense to me. If anyone has a idea please help.
<!DOCTYPE html>
<html>
<head>
<title> Looping assignment
</title>
</head>
<script>
var grades = new Array()
for (i = 0; i < 4; i++) {
grades[i] = parseFloat(prompt("Enter your grades:"))
}
var total = (grades[0] + grades[1] + grades[2] + grades[3])
var average = (total / 4)
if ( average >= 100)
alert("How is the average higher than 100! A+ grade/n " + average);
if ( average > 75 && average <99)
alert("The average is between 75 and 100. B grade /n " + average);
if ( average > 50 && average <75)
alert("The average is between 75 and 50. C grade /n " + average);
</script>
</body>
</html>

You need to use && operator instead of ;
var grades = new Array()
for (i = 0; i < 4; i++) {
grades[i] = parseFloat(prompt("Enter your grades:"));
}
var total = (grades[0] + grades[1] + grades[2] + grades[3])
var average = (total / 4)
if (average >= 100)
alert("How is the average higher than 100! A+ grade/n " + average);
if (average > 75 && average <=99)
alert("The average is between 75 and 100. B grade /n " + average);
if (average > 50 && average <=75)
alert("The average is between 75 and 50. C grade /n " + average);

Related

Finding the grade based on the best score

So my program wants the user to input the number of students and their scores. Based on the best score it will follow this grading scheme:
The score is > or = the best - 10 then the grade is A.
The score is > or = the best - 20 then the grade is B.
The score is > or = the best - 30 then the grade is C.
The score is > or = the best - 40 then the grade is D.
Anything else is an F
This is my code so far:
var readlineSync = require('readline-sync')
let scoreArray = []
let best = 0
students = readlineSync.question('Enter the number of students: ');
for (var x = 0; x < students; x++){
score = readlineSync.question('Enter Score: ')
scoreArray.push(score);
}
for (var i = 0; i < scoreArray.length; i++){
var data = scoreArray[i];
if(best < scoreArray[i]){
best = scoreArray[i];
}
if(scoreArray[i] >= (best-10)){
grade = 'A';
}else if(scoreArray[i] >= (best-20)){
grade = 'B';
}else if(scoreArray[i] >= (best-30)){
grade = 'C';
}else if(scoreArray[i] >= (best-40)){
grade = 'D';
}else {
grade = 'F';
}
console.log('Student ' + i + ' score is ' + scoreArray[i] +' and grade is ' + grade);
}
When I run the code it sometimes displays the correct grade and sometimes it doesn't. It should display this:
Enter the number of students: 4
Enter Score: 40
Enter Score: 55
Enter Score: 70
Enter Score: 58
Student 0 score is 40 and grade is C. Student 1 score is 55 and grade
is B. Student 2 score is 70 and grade is A. Student 3 score is 58 and
grade is B.
Instead it displays the grades A,A,A,B.
It looks like you're iterating over scoreArray and updating best along the way. You should first get the max score from scoreArray so you know the best to start with, and then iterate over scoreArray. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max for more info, but put var best = Math.max(...scoreArray); before your for loop, and then your conditional logic should work.
scoreArray = [49, 81, 72, 80, 81, 36, 58];
var best = Math.max(...scoreArray);
for (var i = 0; i < scoreArray.length; i++) {
var data = scoreArray[i];
if (scoreArray[i] >= (best - 10)) {
grade = 'A';
} else if (scoreArray[i] >= (best - 20)) {
grade = 'B';
} else if (scoreArray[i] >= (best - 30)) {
grade = 'C';
} else if (scoreArray[i] >= (best - 40)) {
grade = 'D';
} else {
grade = 'F';
}
console.log('Student ' + i + ' score is ' + scoreArray[i] + ' and grade is ' + grade);
}

I am trying to get a JavaScript Program to Print a letter grade and Percentage based on a average using a switch statement but the answer comes up NaN

In my program I am using a do-while loop to prompt for grades until a -1 is entered and then increments the amount of grades inputted. A while loop then checks if the grade isNaN or less then 0 or greater then 100. The program will ask for another grade if so. If a -1 is entered the program will break from the loop and calculate the average based on the counter for the grades and the total number of grades added together. When I run the program and put in a negative 1 to stop it prints the -1 inside of the table and the semester average displays as NaN. I am assuming this is because the negative -1 makes it not a non positive number but I am unsure how to fix this.
Here is my code.
do {
grade = prompt('Enter grade(-1 to stop)');
count++;
while (grade < -1 || grade > 100 || isNaN(grade)) {
grade = prompt('Enter grade again (-1 to stop)');
}
document.write('<td>Grade ' + count + '</td>');
document.write('<td width="50"> ' + grade + ' </td>');
document.write('</tr>');
}
while (grade != -1) {
--total;
alert("You have input -1 to stop");
document.write('</table>');
}
total = total + grade;
avg = total / count;
document.write('Semester Average: ' + avg + '<br /><br />');
I tried to fix your problem here
https://jsfiddle.net/51v3qft9/26/
This is the snippet which may not be correct as your logic but it works for me
var count = 0
var total = 0
var avg = 0
document.write('<table>');
do {
grade = prompt('Enter grade(-1 to stop)');
//if user key-in nothing, we need to ask user to key-in again
while (!grade || grade < -1 || grade > 100 || isNaN(grade)) {
grade = prompt('Enter grade again (-1 to stop)');
}
//whenever user key-in -1, we need to break `while`
if (grade == -1) {
break
}
count++;
document.write('<td>Grade ' + count + '</td>');
document.write('<td width="50"> ' + grade + ' </td>');
document.write('</tr>');
total += Number(grade); // you need to convert `grade` to number
} while (grade != -1)
alert("You have input -1 to stop");
document.write('</table>');
if(count > 0) {
avg = total / count;
}
document.write('Semester Average: ' + avg + '<br /><br />');
Is this what you want?
https://jsfiddle.net/sean7777/qtf3v4rn/26/
Hope this helps :)

I want to write javascript code to get a addition countdown

so basically this the prompt:
Addition countdown
you enter a number and the code should be adding a number while countingdown, for example if the user enter 10, then the result should be:
10 + 9 + 8 + 7 + 6 + 5 + 4 +3 +2 +1=55.
This is what I have so far:
var num = Number(prompt("Enter a Number Greater than zero"));
while (num > 0){
first = num;
second = num-=1;
document.write(first + " +" + second + " +");
value = first + num;
document.write(value)
num--;
}
but I keep on getting something like this:
4 +3 +72 +1 +3 (let's say 4 is the number the user inputs)
I'm stuck can someone please help me????!!
You can keep total in one variable outside of while loop.
var num = Number(prompt("Enter a Number Greater than zero"));
var total = 0;
while (num > 0) {
total += num;
document.body.innerHTML += (num == 1 ? num + ' = ' + total : num + ' + ');
num--;
}
You could change the algorithm a bit, because for the first value, you need no plus sign for the output.
var num = Number(prompt("Enter a Number Greater than zero")),
value = 0;
document.body.appendChild(document.createTextNode(num));
value += num;
num--;
while (num > 0) {
document.body.appendChild(document.createTextNode(' + ' + num));
value += num;
num--;
}
document.body.appendChild(document.createTextNode(' = ' + value));

Trying to find the average

I need to know why when I try to divide student 1,student 2 and student 3. I think it might be a looping error since the number that it give's me is numbering in the thousands but I don't see how that could be happening.
function averageOfThreeScores() {
var student1;
var student2;
var student3;
var end;
do {
student1 = prompt("What are the scorces of students 1?");
student2 = prompt("What are the scorces of students 2?");
student3 = prompt("What are the scorces of students 3?");
end = prompt("Would you like to end, type yes to end.");
var average = (student1 + student2 + student3) / 3;
if (average <= 59) {
document.write(average + " Your score is F <br/>");
} else if (average <= 69) {
document.write(average + " Your score is D <br/>");
} else if (average <= 79) {
document.write(average + " Your score is C <br/>");
} else if (average <= 95) {
document.write(average + "That's a great score <br/>");
} else if (average <= 100) {
document.write(average + "God like </br>");
} else {
document.write(average + " End <br/>");
}
}
while (end != "yes");
}
You expect the sum of student grades to be number but in fact they are concatenated as string. So if user types following values for example:
for student1: 13
for student2: 36
for student3: 50
The average will be 44550 because the sum will be (concatenated strings): 133650
To fix this, just convert the type to number when you get it.
student1 = parseInt(prompt("What are the scorces of students 1?"));
student2 = parseInt(prompt("What are the scorces of students 2?"));
student3 = parseInt(prompt("What are the scorces of students 3?"));

Find the sum of a Javascript array and divide by its length

I'm almost embarrassed to ask this.
I'm a beginner programmer, and Javascript is very confusing to me. I managed to put together this much with the help of my instructor, but there are some simple things I can't get right.
I tried search Stack Overflow for a thread that would answer my question, but all of them I've seen contain code that I haven't learned about yet, so they're all just gibberish to me.
What I'm trying to do is add all the values of an Array and divide the sum by the array's length, ergo, find the average. The description of the assignment is find the average of any number of students' grades.
My two problems are
I can't figure out how to get the sum of all numeric values in the Array and,
For some reason, array.length returns one more than the actual length of the Array, even if I add a -1. (ex. if I enter 6 values, the array.length would return 7.)
I know where the problem is but I can't figure out what I need to enter. This assignment is due tomorrow, so anyone's time and effort is appreciated.
Here is my script:
<script type="text/javascript">
var allGrades = new Array();
var g = 0;
var l = 0;
var s = 0;
var t = 0;
do {
allGrades[g] = window.prompt("Please enter one grade for each window. After you enter a grade, enter an 'x' to see the average of the grades you entered.", "")
g++;
}
while (allGrades[g - 1] != "x")
for (l = 0; l < allGrades.length - 1; l++) {
s += allGrades[l] // Where I think the problem is
}
t == s / g - 1;
g == allGrades.length - 1; //
window.alert(g)
switch (t) {
case (t >= 90):
window.alert("Your average grade is " + (t) + ". " + "This is an A.")
break;
case (t >= 80 && t < 90):
window.alert("Your average grade is " + (t) + ". " + "This is a B.")
break;
case (t >= 70 && t < 80):
window.alert("Your average grade is " + (t) + ". " + "This is a C.")
break;
case (t >= 60 && t < 70):
window.alert("Your average grade is " + (t) + ". " + "This is a D.")
break;
case (t <= 60):
window.alert("Your average grade is " + (t) + ". " + "This is a failing grade.")
break;
}
</script>
I'm sorry if what I'm asking seems dumb. I've only been taking web programming for about two months, so I could really use some help!
Kyle
== is the comparison operator. You need to use the assignment operator (=) here:
t==s/g-1;
And the lines near it.
Also, for your own sake, do not use single-letter variable names unless you have a good reason for doing so.
Here's a cleaner way of writing the script:
var grades = [];
do {
var input = window.prompt("Please enter one grade for each window. After you enter a grade, enter an 'x' to see the average of the grades you entered.", "");
grades.push(parseFloat(input));
} while (input != 'x');
var sum = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[l];
}
var average = (sum / grades.length) * 100;
var grade;
if (average >= 90) {
grade = 'A';
} else if (average >= 80) {
grade = 'B';
} else if (average >= 70) {
grade = 'C';
} else if (average >= 60) {
grade = 'D';
} else {
grade = 'failing grade';
}
alert('Your average grade is ' + average + '. ' + 'This is a ' + grade);
t==s/g-1;
g==allGrades.length-1; //
Are both Comparisons, for assignment they should be
t=s/g-1;
g=allGrades.length-1;

Categories

Resources