使用javascript获取单词数量

时间:2014-07-30 15:42:05

标签: javascript facebook

嘿伙计们正在开发一个Facebook应用程序,其中计算消息中的单词数量。

我的代码的主要部分是

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>


  function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);

if (response.status === 'connected') {
  // Logged into your app and Facebook.
  testAPI();
} else if (response.status === 'not_authorized') {
  // The person is logged into Facebook, but not your app.
  document.getElementById('status').innerHTML = 'Please log ' +
    'into this app.';
} else {

  document.getElementById('status').innerHTML = 'Please log ' +
    'into Facebook.';
}


}




  function checkLoginState() {
FB.getLoginStatus(function(response) {
  statusChangeCallback(response);
});


}



  window.fbAsyncInit = function() {
  FB.init({
appId      : '289533237896176',
cookie     : true,  // enable cookies to allow the server to access
                    // the session
xfbml      : true,  // parse social plugins on this page
version    : 'v2.0' // use version 2.0
  });
 };










  (function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));




  function testAPI() {
console.log('Welcome!  Fetching your information.... ');
FB.api('/me/inbox',function(response) {  for (var i=0;i<response.data.length;i++) {
var thread = response.data[i];

for (var j=0;j<thread.comments.data.length;j++) {
    var comment = thread.comments.data[j].message;
console.log(comment); //prints  http://pastebin.com/4EaStYMX
}

}



}



);



}



</script>




<fb:login-button scope="public_profile,email,read_mailbox" onlogin="checkLoginState();">

</fb:login-button>





<div id="status">



<div id="fb-root"></div>

</div>



</body>

</html>

这是http://pastebin.com/4EaStYMX

的输出

我需要的是我必须从信息中找到所有单词。

我尝试了很多方法,但它没有用。希望你们能帮帮我

提前致谢..

1 个答案:

答案 0 :(得分:0)

正如评论中所提到的,要计算每条消息中的单词数,只需将每条消息传递给另一个计算它们的函数:

function countNumberOfWordsInMessage(message) {
    var numWords;

    // Trim extra spaces
    message = message.trim();

    // Separate words by space, and count total words
    numWords = message.split(' ').length;

    return numWords;
}

测试小提琴:http://jsfiddle.net/fM7Z7/