javascript - If/else statement goes straight to else -
my if/else statement goes straight else , cant seem figure out why. here code:
var sentiment = require ('sentiment'); var twittersentiment; var geocolor; var results; var twit = new twitter({ consumer_key: credentials.consumer_key, consumer_secret: credentials.consumer_secret, access_token_key: credentials.access_token_key, access_token_secret: credentials.access_token_secret }); twit.stream( 'statuses/filter', { 'locations': location }, function(stream) { stream.on('data', function(tweet) { console.log(tweet.text); results = sentiment (tweet.text); twittersentiment = results; //comparison of sentiment scores if (twittersentiment == 0) { geocolor = '#b5b5b5'; } else if (twittersentiment < 0) { geocolor = '#fc0828'; } else { geocolor = '#00de1e'; } console.log (geocolor); }); }); this example output:
omg yes!! #00de1e think understand? want ask mine same question. not sure i'm ready have conversation. #00de1e thing of beauty by: @youtube #00de1e still this?? #00de1e as can see tweets being identified 1 color; if if/else statement not implemented correctly?
when change console.log (geocolor); console.log (results); output:
omg yes!! { score: 1, comparative: 0.25, tokens: [ 'omg', 'yes' ], words: [ 'yes' ], positive: [ 'yes' ], negative: [] } think understand? want ask mine same question. not sure i'm ready have conversation. { score: 1, comparative: 0.041666666666666664, tokens: [ 'do', 'you', 'think', 'will', 'actually', 'understand', 'i', 'want', 'to', 'ask', 'mine', 'the', 'same', 'question', 'just', 'not', 'sure', 'i\'m', 'ready', 'to', 'have', 'that', 'conversation' ], words: [ 'want' ], positive: [ 'want' ], negative: [] } thing of beauty by: @youtube { score: 3, comparative: 0.25, tokens: [ 'a', 'thing', 'of', 'beauty', 'by', 'youtube', ], words: [ 'beauty' ], positive: [ 'beauty' ], negative: [] } still this?? { score: 0, comparative: 0, tokens: [ 'do', 'you', 'still', 'do', 'this' ], words: [], positive: [], negative: [] } as can see each tweet has individual sentiment score of respectively 1,1,3,0 why if/else statement disregarding numbers?
what can change in code if/else statement correctly implements , considers sentiment score of tweets? goal output appropriate color each tweet.
you setting twittersentiment result object , comparing whole object instead of score. change code to:
if (twittersentiment.score == 0) { geocolor = '#b5b5b5'; } else if (twittersentiment.score < 0) { geocolor = '#fc0828'; } else { geocolor = '#00de1e'; }
Comments
Post a Comment