0

This is for my GCSE Computer Science. Here's the code:

<html>
<p id="demo"></p>
<script>
var middaytemp = []; midnighttemp = [];
for (count = 0; count <= 29; count++) {
middaytemp[count] = prompt("What was the temperature at midday");
midnighttemp[count] = prompt("what was the temperature at midnight?");
document.getElementById("demo").innerHTML += middaytemp[count] + "&deg; Celcius<br>";
document.getElementById("demo").innerHTML += midnighttemp[count] + "&deg; Celcius<br>";
}
</script>
</html> 

As you can see it asks these two questions 29 times each. I've got them in an array but I need to find the average of the array. so when the temperatures are put in at the end of the code it shows the average for the night and average for the day.

5
  • 2
    Those questions are asked thirty times each. But where are you stuck? What part(s) do you not understand how to implement? Commented Jan 14, 2016 at 19:43
  • Celsius, not Celcius Commented Jan 14, 2016 at 19:45
  • Yeah I know. That was a typo. I need to find the average and output it. Thanks again. Luke Commented Jan 14, 2016 at 19:48
  • Sum the values inside the for loop with something like middaytotal and midnighttotal. Then after the loop you calc the average, no aditional loops or functions are needed. Commented Jan 14, 2016 at 19:49
  • Which average? Arithmetic? Geometric? Harmonic? Power? A composition of them, e.g.: arithmetic-geometric (AGM)? Median? Mode? Commented Jan 14, 2016 at 19:50

4 Answers 4

2

Use a reduce! This will return the sum of midday into sum.

let sum = midday.reduce((x,y) => x+y);
sum = sum/midday.length; //average

I used a an ES6 feature here called a lambda expression or "fat arrow function". It is the lexical equivalent of an anonymous function, which is used as the callback parameter for the array reduce function.

var sum = midday.reduce(function(x,y) {
    return x+y;
});

With a lambda expression, the return is implied, which is why you only need => x+y. Multiple lines would require { ... }. Here is the documentation for arrow function syntax.

Sign up to request clarification or add additional context in comments.

4 Comments

Look at the OP's code; you need to explain what Array.prototype.reduce() does, and what arrow functions are, and how they work.
Slight point of clarification "With a lambda, the return is implied", should probably say something like "With a single expression lambda".
or just remove all of that from the answer, leaving the last snippet since the rest is irrelevant, evil grin
@KevinB you get out of here! Go on, get!
1

Just iterate through the array, since your values are strings, they have to be converted to an integer first. And average is just the sum of values divided by the number of values.

var sum = 0; for( var i = 0; i < elmt.length; i++ ){ sum += parseInt( elmt[i], 10 ); //don't forget to add the base } var avg = sum/elmt.length; document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );

5 Comments

How do you iterate through the array? Could you give an example? Thanks again. Luke
var sum = 0; for( var i = 0; i < elmt.length; i++ ){ sum += parseInt( elmt[i], 10 ); //don't forget to add the base } var avg = sum/elmt.length; document.write( "The sum of all the elements is: " + sum + " The average is: " + avg );
^sorry - not used to using the correct formatting. I hope it still makes sense.
@RichardDickins you should edit that into your answer, not as a comment so that it's easier to read.
@SterlingArcher Cheers. Is there any way to send a direct message to one user?
0

You can achieve the results via following

var sumTempDay = 0;

for (var i = 0 ; i < middaytemp.length; i++) {
   sumTempDay += middaytemp[i];

}

var avgTempDay = sumTempDay/middaytemp .length;

var sumTempNight = 0;

for (var i = 0 ; i < midnighttemp.length; i++) {
   sumTempNight += midnighttemp[i];

}

var avgTempNight = sumTempNight/midnighttemp.length;

2 Comments

Thanks a lot. Really helped. I'll do that. Where do I put the prompt("whats the temp at midday"). Sorry. I'm new to this. Thanks. Luke
where do i put the prompt where i ask the temp? sorry i'm new to this. Thanks. Luke
0

You need to first convert the string into float. Then calculate the average.

var sum = 0
middaytemp.forEach(function(val){
    sum += parseFloat(val);
})
var average = sum/middaytemp.length;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.