Skip to content
Advertisement

getting NaN when calculating parsed integer difference [closed]

I have a program that counts the number of lines in a text file and it works fine. What I am trying to do is count the number of lines in 2 different files and calculate their difference but I’m getting NaN I parsed them to integers why is it not a number? How can I calculate their difference? Thanks in advance.

JavaScript

Advertisement

Answer

Lets take a close look at this line

JavaScript

Since I don’t have those var’s, lets replace them with some demo numbers:

JavaScript

This will still return NaN because "Calc :10" - 5 fails.

If you enclose the sum in some brackets, there evaluated before adding to the string so it becomes "Calc :" + 5. Since JS will convert the 5 to a string, it producing the expected output:

JavaScript

So you’re console.log should look something like:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement