Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6578203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:44:26+00:00 2026-05-25T15:44:26+00:00

ok, I have a homework assignment where I have to read in files and

  • 0

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");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:44:27+00:00Added an answer on May 25, 2026 at 3:44 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this homework assignment where the user is asked to input numbers and
For a homework assignment, I have to write a MySQL query to calculate the
Hello I have a homework assignment where I need to read two matrix .txt
I have a homework assignment, and i am finished other then one question (see
I have a homework assignment where I'm supposed to find the cheapest airfares between
I have a homework assignment where I need to take input from a file
I have a homework assignment to sort an array in ascending order. Obviously, this
Hi I have a homework assignment where I need to implement an intersection of
(this is indirectly a part of a much larger homework assignment) I have something
I have to implement an API for a homework assignment, and my instructor has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.