user939287
user939287

Reputation: 109

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
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {
        //function that processes each line into an array
        //with each number as an element and does the euclidean dis.
        processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});


//do the same for the next file
fs.readFile('file2.txt','utf8', function (err, data)
{
    if (err) throw err;
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {

      processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});

//and again
fs.readFile('file3.txt','utf8', function (err, data)
{
    if (err) throw err;
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {

      processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});

//and again
fs.readFile('file4.txt','utf8', function (err, data)
{
    if (err) throw err;
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {

      processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});

//and again
fs.readFile('file5.txt','utf8', function (err, data)
{
    if (err) throw err;
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {

      processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});

//and again
fs.readFile('file6.txt','utf8', function (err, data)
{
    if (err) throw err;
    stuff=data.split('\n');
    for (var i = 0; i < stuff.length; i++)
    {

      processLine(stuff[i]);
    }
    data.length = 0;
    stuff.length = 0;
});

//function to split each line into an array with each number as an element
//then parse the number strings into floats and do the euclidean distances,
//storing the values in arrays for each bone.
function processLine(line)
{
    var line1 = line
    var numbers = line1.split(" ");
    line1.length = 0;
    for (var i = 0; i < numbers.length; i++)
    {
        var number = parseFloat(numbers[i]);
        line1[i] = number[i];
    }
        lHipJoint = Math.sqrt((line1[6] - line1[9])*(line1[6] - line1[9]) + (line1[7] - line1[10])*(line1[7] - line1[10]) + (line1[8] - line1[11])*(line1[8] - line1[11]));

    //reset the arrays so they can be reused
    line1.length = 0;
    numbers.length = 0;
    number.length = 0;
}

//calculations and output for the mean and SD of each bone's distance from the root bone.
for(var i = 0; i < lHipJoint.length; i++)
{
    var lHipJointTotal = lHipJointTotal + lHipJoint[i];
}

var lHipJointMean = lHipJointTotal/lHipJoint.length;

for(var i = 0; i < lHipJoint.length; i++)
{
    var lHipJointSDSum = lHipJointSDSum + (lHipJoint[i] - lHipJointMean)*(lHipJoint[i] - lHipJointMean);
}

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");

Upvotes: 1

Views: 2466

Answers (1)

megakorre
megakorre

Reputation: 2223

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 < list.length; i++) {
 x = x + ......
}

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

lHipJoint.push(Math.sqr(........

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.

Upvotes: 2

Related Questions