In: Computer Science
Write a JavaScript program that computes the average of a collection of numbers and then outputs the total number of values that are greater than the average.
An A grade is any score that is at least 20% greater than the average. The B grade is any score that is not an A, but is at least 10% greater than the average. An F grade is any score that is at least 20% less than the average. The D grade is any score that is not an F, but is at least 10% less than the average. Any scores that are within 10% of the average (either less than or greater than) are C's.
var arry = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
var sum = 0;
for( var i = 0; i < arry.length; i++ ){
sum = sum + arry[i]; //computing the sum of numbers
}
//computing the average
var avg = sum/arry.length;
var count = 0;
for( var i = 0; i < arry.length; i++){
if( arry[i] > avg)
count = count + 1; //number of scroes greater than average
}
document.write( "The number of values greater than average are " + count);
document.write("<br>")
//computing the grades for the respective scores
for( var i = 0; i < arry.length; i++){
document.write( "Score "+ arry[i] +" has ");
if( arry[i] > (avg * 1.2) ){
document.write("Grade A");
} else if(arry[i] > (avg * 1.1)){
document.write("Grade B");
} else if(arry[i] <= (avg * 0.8)){
document.write("Grade F");
} else if(arry[i] <= (avg * 0.9)){
document.write("Grade D");
}else{
document.write("Grade C");
}
document.write("<br>")
}
Screenshot of the program:
Output of the program:
Hoping that the solution helps !!