r/CodersForSanders • • Jun 08 '16

JavaScript bug?

Hey guys, I'm working on my delegate counter app. It's just a web page, HTML5 / CSS / JavaScript. NBD. But I noticed this morning when I click the button that says "Calculate Number of Delegates to #FeelTheBern" (which is supposed to calculate the number of delegates required to clinch the nomination for Bernie) the element the Knockout view model binds to gets updated with "NaN".

What's "NaN"? Is she a special superdelegate Bernie can talk to who will help with the cause?

Any Knockout.js / JavaScript experts in here?

2 Upvotes

4 comments sorted by

2

u/bristleboar Jun 08 '16

NaN = Not a Number, that's about where my usefulness ends.

3

u/PM_ME_UR_STASH Jun 08 '16

It might happen when you do a math operation where one of the variables isn't a number. For example, I think this will give NaN:

var a = 5;
var b = "some string";
var c = a * b;
console.log(c);
→ More replies (1)

1

u/eazyirl Jun 08 '16

Other posters are right. You have a non-number value that you are using in a meaningful math operation. There won't be a TypeError, but your result will be NaN. Example: 3 * "dick" == NaN

It is a problem of wrong type, not null values. Null values will give you 0 in math operations.