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
alexlitel
25,059 PointsHow do I combine variables from two separate JSON sources?
I am trying to make a simple jQuery plugin in that returns the values of the number of Facebook likes and the number of tweets there are containing a given link.
I'm able to get the numbers for both the Facebook like count and Twitter shares, but I cannot seem to figure out how to combine the two values. Could anyone help?
/*
To do:
1. Combine tweet count and Facebook like count.
2. Generate new windows for tweet/facebook share on click or selection of Twitter/FB child list items.
*/
var shares;
var count;
var combinedLikes = shares + count;
var urlCurrent = 'http://www.google.com';
var twitterCountUrl = 'http://urls.api.twitter.com/1/urls/count.json?url=' + urlCurrent + '&callback=?';
var facebookCountUrl = 'https://graph.facebook.com/fql?q=SELECT%20share_count,%20like_count,%20comment_count,%20total_count,commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27' + urlCurrent + '%27';
function getnumString(num) {
var numString;
if (num < 1000) {
numString = num;
} else if (num < 10000) {
numString = num.charAt(0) + ',' + num.substring(1);
} else if (num < 1000000) {
numString = (Math.round((num / 1000) * 10) / 10) + "k"
} else {
numString = (Math.round((num / 1000000) * 10) / 10) + "M"
}
return numString.toString();
}
function twitterCount(data) {
count = parseFloat(data.count);
$('.twitter').append('<p class="num">' + getnumString(count) + '</p>');
return count;
}
function facebookCount(data) {
shares = parseFloat(data.data[0].like_count);
$('.facebook').append('<p class="num">' + getnumString(shares) + '</p>');
return shares;
}
$.getJSON(twitterCountUrl, twitterCount);
$.getJSON(facebookCountUrl, facebookCount);
$('.share').append('<p class="num">' + getnumString(combinedLikes) + '</p>');
console.log(combinedLikes);
console.log(typeof combinedLikes);
$('#social ul').on('mouseover click focusin', function() {
$('.facebook, .twitter').slideDown("slow");
}).on('mouseleave focusout click', function() {
$('.facebook, .twitter').hide();
});
$('#social .twitter').on('mouseenter focusin', function() {
$('#social .twitter').html('<p class="changed">TWEET<br>TWEET<br>TWEET</p>');
}).on('mouseleave focusout', function() {
$('#social .twitter').html('<p> tweets</p>');
$('.twitter').append('<p class="num">' + getnumString(count) + '</p>');
});
$('#social .facebook').on('mouseenter focusin', function() {
$('#social .facebook').html('<p class="changed">POST<BR>POST<BR>POST</p>');
}).on('mouseleave focusout', function() {
$('#social .facebook').html('<p> likes</p>');
$('.facebook').append('<p class="num">' + getnumString(shares) + '</p>');
});
console.log(shares);
console.log(count);
1 Answer
Hayley Swimelar
7,124 PointsIn your twitterCount and facebookCount functions you're returning the number without assigning it to anything.
You should check out this stackoverflow post on how to do this.