5

I am trying to increment a variable using the ++ operator but I keep getting NaN as a result and I'm not sure why. Here is my code:

var wordCounts = { };
var x = 0
var compare = "groove is in the heart";
        var words = compare.split(/\b/);
        for(var i = 1; i < words.length; i++){
            if(words[i].length > 2){
                wordCounts["_" + words[i]]++;
            }
        }


alert(wordCounts.toSource());
laurent
  • 88,262
  • 77
  • 290
  • 428
mcgrailm
  • 17,469
  • 22
  • 83
  • 129
  • The sample has an off-by-one error: the first index of `words` is 0, not 1. Also, should not "I" and "a" count as words? You can change the regexp so as to filter out the non-word characters, thus removing the need for the `words[i].length > 2` check. See if you can figure how. – outis Nov 29 '11 at 05:06
  • yeah the 1 was me just trying stuff, actually I need to exclude any words less than 3 characters long and I'll also have to filter out the words "was" ,"the","and" words like that. – mcgrailm Nov 29 '11 at 13:57

5 Answers5

10

The value of wordCounts["_" + words[i]] is initially undefined so when you ++ it, it gives you NaN. Just change your code to:

if (wordCounts["_" + words[i]]) {
    wordCounts["_" + words[i]]++;
} else {
    wordCounts["_" + words[i]] = 1;
}
laurent
  • 88,262
  • 77
  • 290
  • 428
  • Or `wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;` – nnnnnn Nov 29 '11 at 05:11
  • 2
    Yes, there are many ways to write it. Putting the key `"_" + words[i]` into a variable would also be a good idea for readability and a small optimization. – laurent Nov 29 '11 at 05:12
  • Yep. +1 for being first with both an explanation of the problem _and_ a solution. – nnnnnn Nov 29 '11 at 05:26
2

What you're basically doing is

undefined++

Which will result in...

NaN

Try...

wordCounts["_" + words[i]] = (wordCounts["_" + words[i]]++ || 1);

Since NaN is a "falsey" value the || will fall back to 1.

Chad Moran
  • 12,834
  • 2
  • 50
  • 72
  • Please don't do `x = x++` type statements, they always lead to confusion. (In some languages - not sure about JS - the result is not even defined in the language spec.) – nnnnnn Nov 29 '11 at 05:21
2

Try something like...

var key = "_" + words[i];

if (wordCounts[key]) {
    wordCounts[key]++
} else {
    wordCounts[key] = 1;
}

You are trying to increment undefined which is giving you NaN.

alex
  • 479,566
  • 201
  • 878
  • 984
1

To be able to use the ++ operator (which takes a number and increments it by one) the target needs to have a number first.

Attempt a check to see if the object is defined, and if not initialize it by setting it's value to 1.

if ('undefined' === typeof wordCounts["_" + words[i]]) {
            wordCounts["_" + words[i]] = 0;
}

Something like:

var wordCounts = {};
var x = 0
var compare = "groove is in the heart";
var words = compare.split(/\b/);
for (var i = 1; i < words.length; i++) {
    if ('undefined' === typeof wordCounts["_" + words[i]]) {
        wordCounts["_" + words[i]] = 0;
    }
    if (words[i].length > 2) {
        wordCounts["_" + words[i]]++;
    }
}
alert( JSON.stringify( wordCounts ) );
buley
  • 28,032
  • 17
  • 85
  • 106
0

You're trying to increment an object (wordCounts[]++) it's not a number so it can't be incremented, which is why you're getting that error. What are you actually trying to do (in plain English)?

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • so how can i make this work since I don't know what the words are going to be – mcgrailm Nov 29 '11 at 05:07
  • again, I'm asking that you ask a question using plain English syntax as to what you're trying to accomplish here? What do you believe is stored in wordCounts[] and what do you expect to see happen when you increment (++) it? You say it's not "working" What would it be doing if it WAS working? – Yevgeny Simkin Jun 06 '16 at 21:38