【问题标题】:If/else statement goes straight to elseif/else 语句直接转到 else
【发布时间】:2016-11-11 01:48:55
【问题描述】:

我的 if/else 语句直接指向 else,我似乎不知道为什么。这是我的代码:

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);         
        });
    });

这是一个示例输出:

 omg yes!!
#00DE1E
Do you think will actually understand? I want to ask mine the same question. Just not sure I'm ready to have that conversation.
#00DE1E
 A thing of beauty by:
 @youtube
#00DE1E
do you still do this??
#00DE1E

如您所见,所有推文都仅由一种颜色标识;就好像我的if/else 语句没有正确实现一样?

当我将console.log (geoColor); 更改为console.log (results); 这是我的输出:

   omg yes!!
{ score: 1,
  comparative: 0.25,
  tokens: [ 'omg', 'yes' ],
  words: [ 'yes' ],
  positive: [ 'yes' ],
  negative: [] }

 Do you think will actually understand? I want to ask mine the same question. Just not sure I'm ready to have that 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: [] }

A thing of beauty by:
 @youtube
{ score: 3,
  comparative: 0.25,
  tokens: 
   [ 'a',
     'thing',
     'of',
     'beauty',
     'by',
     'youtube', ],
  words: [ 'beauty' ],
  positive: [ 'beauty' ],
  negative: [] }

do you still do this??
{ score: 0,
  comparative: 0,
  tokens: 
  [
     'do',
     'you',
     'still',
     'do',
     'this' ],
  words: [],
  positive: [],
  negative: [] }

如您所见,每条推文的个人情绪得分分别为 1,1,3,0 那么为什么我的 if/else 语句会忽略这些数字?

我可以在我的代码中进行哪些更改,以便我的 if/else 语句正确实施并考虑推文的情绪得分?我的目标是为每条推文输出适当的颜色。

【问题讨论】:

  • 所以你是在比较一个对象和一个数字?
  • 所以你想要分数?? twitterSentiment = results.score;??
  • 正如 zerkms 所说,twitterSentiment 是一个 object ...该对象不等于 0。该对象不小于零,因此执行最终的 else。在对象与整数上使用数值比较甚至没有意义。也许你想比较一下twitterSentiment.score == 0

标签: javascript node.js


【解决方案1】:

您的 var results 是一个对象,其中包含许多其他属性。在 javascript 中,“if”子句将始终为非空对象实例返回“true”。将对象与数字进行比较时 - 非原始对象的任何实例都将返回 1

正如你所说,你想比较你的 score 属性的值,所以你需要做的是在你的 if 子句中引用你的 results.score

改成

 twitterSentiment = results.score;

应该可以解决您的问题。

【讨论】:

  • 建议您在回答时添加一些指导,而不仅仅是发布解决方案。
【解决方案2】:

您正在将 twitterSentiment 设置为结果对象并比较整个对象而不仅仅是分数。将您的代码更改为:

if (twitterSentiment.score == 0) {
    geoColor = '#B5B5B5';
} 

else if (twitterSentiment.score < 0) {
    geoColor = '#FC0828';
} 
else {
   geoColor = '#00DE1E';
}

【讨论】:

    猜你喜欢
    • 2019-04-09
    • 2018-01-11
    • 1970-01-01
    • 2017-02-05
    • 2014-11-11
    • 2015-05-16
    • 1970-01-01
    相关资源
    最近更新 更多