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 trial
stefanie guerrero
Courses Plus Student 292 PointsPlease help don't understand why my code isn't working
var friends = [0,1,2,3,4,5]; var sum = 0;
function totalFriends(friends) {
for (var i = 0; i<friends.length; i++) {
totalFriends=+friend[i]; {
}
} }
console.log("Total friends coming to the party is" +totalFriends);
3 Answers
Austin Britton
6,531 PointsHey There! Excellent catches for sure on the semi colon.
I have a feeling there may be some larger issues at play here. If this is exactly how you have the script constructed, I would like to offer a few suggestions.
As far as I can tell, at no point does your function ever get called to invoke the loop to begin the calculation. You can address this by changing your console.log() statement. At the end of the statement, there is a reference to the variable totalFriends, which might be returning zero. You can swap that variable for a function call.
console.log("total number of friends coming to the party is: " + yourFunctionCallHere(arrayOfFriendsHere));
I will address another way to handle calling your function at the end!
I would also caution you to change the name of your function, or the variable. From what I understand, some browsers don't respond well to this.
You may find that your loop still isn't preforming as expected.
Inside your loop when you address the totalFriends variable, it's not quite the one were looking for, this is a scope issue. You will either want to pass in the variable totalFriends as a parameter to your function, or you can create a local variable inside your function, preform and store your calculation within the local variable, and then return your local variable to your totalFriends global variable.
I copied your code and made appropriate changes to test these ideas and came up with 15 guests, heck of a party if id say!
const friendCountArray = [0,1,2,3,4,5]; var totalFriends = 0;
function getTotalFriendsCount(friendCountArray){
let totalFriends = 0;
for(var i = 0; i < friendCountArray.length; i++){
totalFriends += friendCountArray[i];
console.log(i + "and : " + totalFriends);
}
return totalFriends
};
console.log("the totalFriends coming to the party is... " + getTotalFriendsCount(friendCountArray));
or
const friendCountArray = [0,1,2,3,4,5]; var totalFriends = 0;
function getTotalFriendsCount(friendCountArray){ let totalFriends = 0; for(var i = 0; i < friendCountArray.length; i++){ totalFriends += friendCountArray[i]; } return totalFriends };
totalFriends = getTotalFriendsCount(friendCountArray);
console.log("the totalFriends coming to the party is... " + totalFriends);
I hope this helps!
Austin Britton
6,531 Pointsalso when you changed the inside of your function to
totalFriends=+friend[i];
friend doesn't exist. and =+ will not preform the same way that += preforms, which is the one your looking for here.
its declared as friends above in the global scope, and also as the parameter to the function. So when you attempt to reference it, you'll wanna grab it by the same name. Specifically, the name associated with the parameter to the function, sense the variable being passed will be arbitrary
you'll wanna reference it as totalFriends += friends[i];
I would incorporate this into the solution I added below! :)
stefanie guerrero
Courses Plus Student 292 PointsThank you Austin your explanation was very clear and helped understand what I was doing wrong.
Steven Parker
243,656 PointsYou seem to have a typo.
There a stray semicolon (";") between your loop and the bracket that starts the code block it controls (or would otherwise).
Also, you can't have a variable and a function with the same name (totalFriends). You'll need to rename one or the other.
Variables should always be declared before they are assigned.
The concatenating assignment operator is "+=", not "=+".
stefanie guerrero
Courses Plus Student 292 PointsHello Rebecca,
Thanks for reaching out! I uploaded the wrong code and made a few changes. I am having trouble with adding the sum of these elements.
Rebecca Jensen
11,580 PointsRebecca Jensen
11,580 PointsTo make your code readable for others, use three "back ticks"- not apostrophes- which are in the upper left corner of the keyboard; hit return, paste code, hit return, and three more back ticks.
Did you mean to put this INSIDE your for loop?
totalFriends+=friendCountArray[i];There is currently nothing between the brackets {} of your for loop.