Hi, i need help creating a fuel volume calculator to estimate the gallons of fuel
required to travel 5 distances
so far the 5 distances in miles the user enters are stored in an array,
however i need a sum to find the average of the five numbers
here is the code:
<!DOCTYPE html
<html
<body>
<h1>Referred Portfolio Part 1</h1>
<head>
<title>Referred Portfolio Part 1</title>
<src="javascript.js"></script>
<script>
var name = window.prompt("What is your name?")
var fuel = window.prompt("Please enter your car's fuel consumption in miles per gallon")
var distance,miles = [];
var sum = 0;
for(var i = 0; i < 5; i++){
distance = window.prompt("Please enter distance in miles");
miles.push(distance);
}
console.log(miles);
document.write("Fuel calculation for "+name + "<br />Fuel consumption: " + fuel + "<br />Average distance travelled: "+distance);
</script>
</head>
</body>
</head>
</html>
mehul12345
0
Newbie Poster
Recommended Answers
Jump to PostAn average is just the total sum of the elements divided by the number of elements. increment your sum variable by the entered value inside the loop and then, once the loop is finished divide it by 5.
All 4 Replies
hericles
289
Master Poster
Featured Poster
mehul12345
0
Newbie Poster
hericles
289
Master Poster
Featured Poster
mehul12345
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.