Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialCalvin Hu
670 PointsHow to find average using for in loop
Helppp, so confusing. Also what is wrong with the ; ?
NSArray *temps = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:75.5],
[NSNumber numberWithDouble:83.3],
[NSNumber numberWithDouble:96],
[NSNumber numberWithDouble:99.7],
nil];
float average = nil;
float runningTotal;
for (NSArray *in temps){
};
1 Answer
landonferrier
25,097 PointsCalvin, here is the code that I got to pass the challenge:
NSArray *temps = [NSArray arrayWithObjects:
[NSNumber numberWithDouble:75.5],
[NSNumber numberWithDouble:83.3],
[NSNumber numberWithDouble:96],
[NSNumber numberWithDouble:99.7],
nil];
float average = nil;
float runningTotal;
for (NSNumber *a in temps){
runningTotal += [a floatValue];
};
average = (runningTotal / temps.count);
In order to add the values to the total, we need to get them in the form of an NSNumber instead of an array, because an array is what we are looping through. Then, the average is equal to the total divided by the number of terms in the array.
If you have any questions, feel free to ask.