Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Columns average of HTML table in vanilla javascript

I have the following js function that once pressed it generates a table.

function calculateMatrixFact() {

            var cache = CacheValues();

            // split the matrix into arrays one for each user and movie
            var user_matrix = createGroups(cache.mu, 4);
            // console.log(user_matrix);
            var score_matrix = createGroups(cache.ms, 3);
            //console.log(score_matrix);

            // remove the string user_name and movie_name
            for (let i = 0; i < user_matrix.length; i++) {
                user_matrix[i].shift();
            }

            for (let j = 0; j < score_matrix.length; j++) {
                score_matrix[j].shift();
            }

            var dot_matrix = [];

            // perform the dot product
            for (let j = 0; j < user_matrix.length; j++) {
                for (let k = 0; k < score_matrix.length; k++) {
                    //console.log(user_matrix[j])
                    //console.log(score_matrix[k])
                    var dot_product = (math.multiply(user_matrix[j], score_matrix[k])).toFixed(1);
                    //console.log(dot_product)
                    dot_matrix.push(dot_product);
                }
            }

            // create the matrix and push back the string (first column of the table)
            var dot_prod_matrix = createGroups(dot_matrix, 5);
            dot_prod_matrix[0].unshift("Anna");
            dot_prod_matrix[1].unshift("Jonny");
            dot_prod_matrix[2].unshift("Kimi");
            dot_prod_matrix[3].unshift("You");
            dot_prod_matrix[4].unshift("Average") //this it the row where I would like to store the avg of each columns.

            // from array to HTML table
            fetch = document.getElementById('matrix_factorization');
            fetch.innerHTML = `<tr>
            <th>User</th>
            <th>Zombieland</th>
            <th>Modern Times</th>
            <th>The Grudge</th>
             </tr>`;

            for (var i = 0; i < dot_prod_matrix.length; i++) {
                var newRow = fetch.insertRow(fetch.length);
                for (var j = 0; j < dot_prod_matrix[i].length; j++) {
                    var cell = newRow.insertCell(j);
                    cell.innerHTML = dot_prod_matrix[i][j];
                }
            }
}

What I am now trying to achieve is to also calculate the avg of each column and store the result in a 5th row that I have defined. What I have tried to do is

var table = document.getElementById("matrix_factorization");
            var avgVal, sumVal = 0;
            var rowCount = table.rows.length - 2
            console.log(rowCount)
            for (var i = 1; i < table.rows.length; i++) {
                sumVal = sumVal + parseInt(table.rows[i].cells[1])
            }
            average = sumVal/rowCount

But when I tried to print the average value I have NaN.

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!