Challenge 10 Megathread

I was thinking to use reduce, but I didn’t know how to solve the problem for wind Speed e
Element in different arrays. Creating an array using map should solve! Thank you for sharing

You can also try using the Array helper function called reduce as well!

Nice job! So basically what you did is create a new array with all the windSpeed from the array of objects, and then reduced them into a one single value using the reduce array function.
Another approach could have been to just use reduce and return the avg:

const avg = windSpeedArr => weatherEntries.reduce((acc, e) => {
return acc + e.windSpeed
}, 0)

return Math.round(avg / weatherEntries.length)

Reduce basically takes in 2 arguments, a callback and a starting value for acc. In our case the starting value was 0, so on the first iteration it would be a=0, b=99 (first element in windspeedarr, now acc is equal to 99. and then it would be 99(acc) + 45(next element in array)…etc. Reduce will always return a reduced value

1 Like

const averageWindSpeed = (weatherEntries) => {
// Code here!
console.log (weatherEntries);
console.log(Math.round(14.74));
// expected output: 15
for (let speed = 0; speed < 14.74; speed++) {
// Runs 5 times, with values of step 0 through 4.
}
console.log(‘averageWindSpeed, speed’);
return speed, weatherEntries;
};

not sure what i’m missing… i get a round number…

I stand corrected. The first variable in the

for (var windSpeed in weatherEntries) { }

is actually an integer, the index of the object in the Array. It is NOT an object.

It was easier for me to understand when written as:

for (var arrayIndex in weatherEntries) { }

Sometimes we choose variable names that make us believe they mean something different.

1 Like

check into iteration of properties of objects… order of iteration is not guaranteed like in an array.

also using the for-in syntax is meant for objects where this order does not matter and the for-of syntax is for arrays.

1 Like

I understood that for loop as an iteration of elements of an Array. The weatherEntries is an Array, which contains Objects.

I’ll read up about the difference between for - in and for - of. Thank you for the tip!

Hey Jamis123,

I just copied your code into my code editor and found a couple of things that may be tripping you up. You definitely have the right idea but just need a little bit more work!

  • First, there is a space between your first console.log and the parenthesis (weatherEntries). It was not showing me the data I needed because of that. That first console.log() will show you that the weatherEntries array actually has two objects inside. This means that you are only using 14.74 - Keep in mind that rounding is one aspect of the challenge but you need multiple values to be able to get some sort of average.
  • Second, you are on the right path with the loop, but make sure you have both values to add up to get the average.
  • Lastly, the last console.log(“averageWindSpeed, speed”) unfortunately won’t work because you are printing that as a string and not the value you intend to see. The terminal would literally print ‘averageWindSpeed, speed’

You are getting very close. Best of luck and happy coding!
Luke :relieved:

1 Like

it depends on the type of for loop… for( const x of array){} is for array and for (const prop in Obj){} is for objects and using this “in” of a for in loop does not guarantee iteration order. Also look at some of the other array methods after you have solved with a for loop that offer cleaner more efficient solutions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for…in

I read, thought that I understood, then ran the code. Now I understand even less.

using for-in passess all tests

let sumWindSpeed = 0;
for (var entry in weatherEntries){
sumWindSpeed = sumWindSpeed + weatherEntries[entry].windSpeed;
}

using for-of fails all tests

let sumWindSpeed = 0;
for (var entry of weatherEntries){
sumWindSpeed = sumWindSpeed + weatherEntries[entry].windSpeed;
}

not wrong, not 100% correct, something to watch out for when iterating through an array. ie. if you were to be comparing array A [1,2,3] to array B [3,2,1] since an array order is specific array A != array B since for in does not adhere to order you could by chance recieve a positive result.

Can any one explain why the length of the array is changed in the 3rd test?
I put the console.log in the beginning before the loop.

console.log("weatherEntries.length = " + weatherEntries.length);

image

BTW, the code is passed all 3 tests

I believe they run multiple tests with different arrays, some contain more objects than others.

2 Likes

Ok. For - in should be out of the question for Arrays.
Why does the for - of not work?

the code for the for-of is incorrect. what is the value of entry at each iteration? entry is not the index nor the value in the for in either. in for in it is accesing that particular element and saying that element inside weather entries then you dot into that element and get value at windspeed key. the for of syntax you are telling it to weatherEntries[entier object found here].windspeed so it does not exist since weatherEntries[entier object found here] is undefined.
the for of correct syntax would have been just entry.windspeed

1 Like

(For … of) worked in my code for this challenge.

your solution works and that is awesome!! just not a reliable use-case and not a good practice thats all.
see response to your other comment Challenge 10 Megathread - #40 by Ermurray

1 Like

I got it now. Thank you. I’m learning more! :slight_smile:

1 Like

When you get unexpected results I recommend you use console.log()

1 Like