WEB230 JavaScript 1 Assignment 3 - Quiz Average In this assignment we will write a JavaScript program to calculate the average of a set of quiz marks. Instructions 1. Create variable containing an array that will contain five or more numbers that represent the marks on quizzes. It is assumed that all quizzes will be out of 10 so the numbers must be values between 0-10. 2. Create a function that will take the array as an argument and return the average of the quizzes. 3. Output a text message with the values of the quizzes, space seperated without brackets. Use a template literal when constructing the output strings (AKA back-ticks, template literals are explained in the section on Strings in Chapter 1). 4. Output a text message with the final average rounded to one decimal place. Again use a teplate literal.
const numbers = [2, 7, 3, 8, 10, 6, 1]
function getAverageValue(arr) {
let sum = 0;
arr.forEach(num => {
sum += num;
});
return sum / arr.length
}
console.log(getAverageValue(numbers))
console.log(`The values of the quizzes: ${numbers.join(' ')}`)
console.log(`Average rounded value: ${getAverageValue(numbers).toFixed(1)}`)
Comments
Leave a comment