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
jason chan
31,009 PointsTotal Difference algo javascript
- With that done, compare the difference between current user's scores against those from other users, question by question. Add up the differences to calculate the
totalDifference.- Example:
- User 1:
[5, 1, 4, 4, 5, 1, 2, 5, 4, 1] - User 2:
[3, 2, 6, 4, 5, 1, 2, 5, 4, 1] - Total Difference: 2 + 1 + 2 = 5
- Remember to use the absolute value of the differences. Put another way: no negative solutions! Your app should calculate both
5-3and3-5as2, and so on. - The closest match will be the user with the least amount of difference.
2 Answers
Thomas Nilsen
14,957 PointsDo you need help or is this some sort of challenge? Anyway:
var arr1 = [5, 1, 4, 4, 5, 1, 2, 5, 4, 1];
arr2 = [3, 2, 6, 4, 5, 1, 2, 5, 4, 1],
totalDifference = 0;
for (var i = 0; i < arr1.length; i++) {
totalDifference += Math.abs(arr1[i] - arr2[i]);
}
console.log(totalDifference); // print 5
jason chan
31,009 Pointsthanks thomas for trying
// data
// but pretend it's not all jason chan and the same image. Getting the match from this giant array of objects.
var friendsData = [
{
"name" : "Jason Chan",
"photo" : "https://pbs.twimg.com/profile_images/621921614377058304/ailqyj6j.jpg",
"scores" : [1,2,3,4,5,1,1,1,1]
},
{
"name" : "Jason Chan",
"photo" : "https://pbs.twimg.com/profile_images/621921614377058304/ailqyj6j.jpg",
"scores" : [3,2,3,4,5,6,7,5,1]
},
{
"name" : "Jason Chan",
"photo" : "https://pbs.twimg.com/profile_images/621921614377058304/ailqyj6j.jpg",
"scores" : [5,2,3,4,5,6,7,5,1]
},
{
"name" : "Jason Chan",
"photo" : "https://pbs.twimg.com/profile_images/621921614377058304/ailqyj6j.jpg",
"scores" : [5,5,6,7,5,6,7,5,1]
}
]
// i need this output though
// NEED this output below or return
var match = {
"name" : "Jason Chan",
"photo" : "https://pbs.twimg.com/profile_images/621921614377058304/ailqyj6j.jpg",
"scores" : [1,2,3,4,5,1,1,1,1]
};
jason chan
31,009 Pointsjason chan
31,009 PointsNeverminds. I figured it outs. Thank bro. ^_^ Much appreciated.