Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Getting NaN for variables in Node.js.. Arg?

Getting NaN for variables in Node.js.. Arg?


ok, I have a homework assignment where I have to read in files and calculate the distance between a bunch of numbers in the files and then print out the mean and standard deviation of each set of numbers. The end of the script, where the console.log stuff is, is giving all NaN for the variables. Can anyone help me out?

*I've omitted repeating parts of the script to make it shorter (their are more arrays than just the lHipJoint array and the calculations for them but I left them out).

var fs = require('fs');

var lHipJoint = new Array();

//open the first text file

fs.readFile('file.txt','utf8', function (err, data)
    if (err) throw err;

    //split the data into an array with each line as an element
    for (var i = 0; i 
Problem courtesy of: user939287


You are doing a lot of strange things here in your script i will try to bring upp as manny as i can.

So first of all dont reset arrays. your in a garbage collected language just reallocate new ones.

Also in the processLine function you are assigning numbers to the indexes of a string i asume you think its an array but its not the same thing. strings are immutable (cant be changed) in javascript.

In the aggregating for loops att the bottom of the file you are declaring the variable in every iteration. you want to declare it before the loop like this.

var x = 0;
for(var i = 0; i 

Your cals to read the files all do the same thing. So you want to use the same function for that. write it ones.

You are assigning to the lHipJoint array in the processLine function my understanding is that you want to add the calculated value to the array. You can do this with the push method like this


Also theres a problem with using the async file reading sins your printing the result before you even read the files. if you want to use the async ones you need to coordinate so that. you only print the result when you done all the file reading. but a tips is to use the non async ones in this case.

I understand this is an assignment so you might not want to read my attempt to correct the program beneath.

Maybe read it after you handed in yours, but im leaving it here for the q&a reference for others reading this.

var fs = require("fs");
var filePaths = ["file.txt", "file2.txt", 
                  "file3.txt", "file4.txt", 
                  "file5.txt", "file6.txt"];

var lHipJoint = [];

filePaths.forEach(function(path) {
  var content  = fs.readFileSync(path, "utf-8");
  var lines = content.split("\n");
  lines.forEach(function(line) {
    if(line.trim() === "") return;
    var numbers = line.split("\t").map(parseFloat);
    // im not touching your calculation :D
    lHipJoint.push(Math.sqrt((numbers[6] - numbers[9])*(numbers[6] - numbers[9]) 
      + (numbers[7] - numbers[10])*(numbers[7] - numbers[10]) + (numbers[8] - numbers[11])
        *  (numbers[8] - numbers[11])));

var lHipJointTotal = lHipJoint.reduce(function(p, c) {
  return p + c;
var lHipJointMean = lHipJointTotal / lHipJoint.length;
var lHipJointSDSum = lHipJoint.reduce(function(p, c) {
  return p + (c - lHipJointMean) * (c - lHipJointMean);
}, 0);
var lHipJointSD = Math.sqrt(lHipJointSDSum/lHipJoint.length);
console.log("The mean distance of the left hip joint from the root bone is " 
             + lHipJointMean + " and the standard deviation is " + lHipJointSD + ".\n");

there might be some error in this program i dont know how the data looks but i hope this helps you.

Solution courtesy of: megakorre


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Getting NaN for variables in Node.js.. Arg?


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription