I attempted to create a grade calculator for any value of grades in JavaScript. No parts of the code are executed when the code is run. Is there a blatant error in my code's syntax? Thanks, Ben
var numberofgrades = prompt("How many grades do you want to calculate?");
var countingvalue = 1;
var grades = [0];
var tempgrade = 0;
var averagetotal = 0;
for(i=0;i<numberofgrades;i++;){
if(countingvalue!=numberofgrades){
var tempgrade= prompt("What is your "+countingvalue"th grade?")
grades.push(tempgrade);
countingvalue++;
}
else if(countingvalue==numberofgrades){
for(var j=1;j=<numberofgrades;j++){
averagetotal = grades[j]+averagetotal;
}
}
alert("Your average grade is: "+(averagetotal/numberofgrades));
Newest edited code-
var numberofgrades = prompt("How many grades do you want to calculate?");
var countingvalue = 1;
var grades = [0];
var tempgrade = 0;
var averagetotal = 0;
for(i=0;i<numberofgrades;i++){
if(countingvalue!=numberofgrades){
var tempgrade= prompt("What is your "+countingvalue+"th grade?")
grades.push(tempgrade);
countingvalue++;
}
else if(countingvalue==numberofgrades){
for(var j=1;j<=numberofgrades;j++){
averagetotal = grades[j]+averagetotal;
}
}
}
alert("Your average grade is: "+(averagetotal/numberofgrades));
My syntax was just fixed for this issue, and it is now running. I am having problems adding two numbers instead of my program concatenating the values. Thanks for your help, Ben
var averagetotal = (grades[j]+averagetotal);
Here is the whole code.
var numberofgrades = prompt("How many grades do you want to calculate?");
var countingvalue = 0;
var grades = [];
var tempgrade = 0;
var averagetotal = 0;
for(var i=0;i<=numberofgrades;i++){
if(countingvalue!=numberofgrades){
if(countingvalue==1){
var tempgrade= prompt("What is your "+(countingvalue+1)+"st grade?");
grades.push(tempgrade);
countingvalue++;
}
else if(countingvalue!=1){
var tempgrade= prompt("What is your "+(countingvalue+1)+"th grade?");
grades.push(tempgrade);
countingvalue++;
}
}
else if(countingvalue==numberofgrades)
{
for(var j=0;j<numberofgrades;j++){
var averagetotal = (grades[j]+averagetotal); // problem line
alert(j+" "+averagetotal); //checking values
}
}
}
alert(grades[0]); //just checking values
alert(grades[1]); //checking values
alert(averagetotal);
alert("Your average grade is: "+(averagetotal/numberofgrades));
you have a syntax error. Instead of
for(i=0;i<numberofgrades;i++;)
it should be
for(i=0;i<numberofgrades;i++)
also instead of
var tempgrade= prompt("What is your "+countingvalue"th grade?")
it should be
var tempgrade= prompt("What is your "+countingvalue+"th grade?")
also instead of
for(var j=1;j=<numberofgrades;j++){
it should be
for(var j=1;j<=numberofgrades;j++){
and finally there seems to be a problem with your brackets - you are missing } at the end
try this code
<script>
var numberofgrades = prompt("How many grades do you want to calculate?");
var countingvalue = 1;
var grades = [0];
var tempgrade = 0;
var averagetotal = 0;
for(var i=0;i<numberofgrades;i++){
if(countingvalue!=numberofgrades)
{
var tempgrade= prompt("What is your "+countingvalue+"th grade?");
grades.push(tempgrade);
countingvalue++;
}
else if(countingvalue==numberofgrades)
{
for(var j=1;j<numberofgrades;j++){
averagetotal = grades[j]+averagetotal;
}
}
alert("Your average grade is: "+(averagetotal/numberofgrades));
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.