Challenge 10 Megathread

Welcome to challenge #10!

Use this thread for any and all questions relating to challenge 10

4 Likes


Not sure what i am doing wrong here but i am unable to get correct output values.

1 Like

for(var windSpeed in weatherEntries) {

Does it help you if I tell you that for the first iteration the var windSpeed is equal to
{temperature:0,weather:"sunny",windDirection: "NNE",windSpeed:24}

and not
windSpeed=24

1 Like

Regardless of the name chosen inside the for loop, the first variable equates to item in weatherEntries, which is an Object.

NOTE: This is not correct. Please see my reply below.

That changes everything

Right that’s what I understand from my understanding

The windSpeed in this case on the first iteration is equal to 0, not an object because of the ‘in’ keyword

1 Like

In a previous challenge I had issues with the way (var e in objArray){} was parsing, and chose a different method of iteration.

Other replies have already hinted, you may want to review which elements of your objects are be observed at what points in your code.

I found it best to just use the same loop as I used yesterday with one minor tweak to total up the values. Then round the calculated average.

Perhaps not the point to use the same way to do something, but I did learn that setting a variable inside the for statement doesn’t allow the index to be used as a number outside the for statement! This was very surprising to me be because other languages allow the index to be used for all sorts of additional functions/calculations!

I ended up creating a loop For/Of to pass windspeed values to my array and then another For - incremental to take the windspeed values inside and add them together, average them, round and then output.

1 Like

can’t see where i went wrong can i get a little help

I don’t think you need to have a new array to get this done. I’m sure it’s possible, but it’s unnecessary.

Just total up the numbers from the existing array, don’t put them in a new array.

Suggestion: do some indentation. I believe that you forgot to declare your local variables.

I made a new array simply to clean it up, my 1 line answer (which passed) was too long for my personal coding preference :sweat_smile:

3 Likes

The information you want to add up is in a property inside each object.

{total = astronautJobs.push(weatherEntries[Index].windSpeed);}

The push() method adds one or more elements to the end of an array and returns the new length of the array.

You are setting the variable total equal to the new length of the array. Which will be 1, 2, 3, 4, etc., not the windSpeed value.

Through some googling, I found using array.prototype.map() to make a new array and then array.prototype.reduce() to get the sum of the numbers made the rest easy.

May be a better way but that’s what worked easiest for me!

I’m struggling a lot to understand how to use the array.prototype it just isn’t making sense to me when I read the documentation. I managed to get the challenge yesterday but frankly I don’t understand what I did. Here is where I’m at with today’s challenge, I feel like I should be passing at least the second test with rounding? What am I missing? Thank you!

const averageWindSpeed = (weatherEntries) => {
  // Code here!
  
  let windSpeedArr=[];
  weatherEntries.forEach(function (speed) {
    windSpeedArr.push(speed.windSpeed);
  });
  
  let avg = windSpeedArr => windSpeedArr.reduce((a,b) => a + b, 0) / windSpeedArr.length;

  //average([99, 45, 26, 7, 11, 122, 22]);

  return Math.round(avg);
}
console.log(averageWindSpeed);

I think to use one line code answer too. But at Larry’s Corner said

…will need to calculate the average wind speed using loops.

So, I did as the proverb

when in Rome, do as the Romans do

1 Like

I found do…while: Loops and iteration - JavaScript | MDN really helpful for this one personally