JavaScript Mark highest and lowest value in HTML table

I have an HTML table and I want to mark cells by adding a class to the highest and lowest value of each column. I have found a few related questions here, but the code misbehaves.

var $table = $("#mytable");
$table.find("th").each(function(columnIndex)
{
    var oldValue=0, currentValue=0, $elementToMark;
    var $trs = $table.find("tr");
    $trs.each(function(index, element)
    {
        oldValue= currentValue;
        var $td = $(this).find("td:eq("+ columnIndex +")");
        if ($td.length!=0) 
        {
            currentValue= parseFloat($td.html());
            if(currentValue > oldValue)
            {
                $elementToMark= $td;
            }
            if(index == $trs.length-1)
            {
              $elementToMark.addClass("highest"); 
            }
        }
        });
});


var $table = $("#mytable");
$table.find("th").each(function(columnIndex)
{
    var oldValue=1000000, currentValue=1000000, $elementToMark;
    var $trs = $table.find("tr");
    $trs.each(function(index, element)
    {
        oldValue= currentValue;
        var $td = $(this).find("td:eq("+ columnIndex +")");
        if ($td.length!=0) 
        {
            currentValue= parseFloat($td.html());
            if(currentValue < oldValue)
            {
                $elementToMark= $td;
            }
            if(index == $trs.length-1)
            {
              $elementToMark.addClass("lowest"); 
            }
        }
        });
});

Here is also a JSFiddle: Link

The problem is that it doesn't mark the right values and I cannot see the reason.

Answer:1

You are updating the oldValue at wrong place.

 var $table = $("#mytable");
    $table.find("th").each(function(columnIndex)
    {
        var oldValue=0, currentValue=0, $elementToMark;
        var $trs = $table.find("tr");
        $trs.each(function(index, element)
        {

            var $td = $(this).find("td:eq("+ columnIndex +")");
            if ($td.length!=0) 
            {
                currentValue= parseFloat($td.html());
                if(currentValue > oldValue)
                {
                    $elementToMark= $td;
                    oldValue= currentValue;
                }
                if(index == $trs.length-1)
                {
                  $elementToMark.addClass("highest"); 
                }
            }
            });
    });


    var $table = $("#mytable");
    $table.find("th").each(function(columnIndex)
    {
        var oldValue=1000000, currentValue=1000000, $elementToMark;
        var $trs = $table.find("tr");
        $trs.each(function(index, element)
        {

            var $td = $(this).find("td:eq("+ columnIndex +")");
            if ($td.length!=0) 
            {
                currentValue= parseFloat($td.html());
                if(currentValue < oldValue)
                {
                    $elementToMark= $td;
                    oldValue= currentValue;

                }
                if(index == $trs.length-1)
                {
                  $elementToMark.addClass("lowest"); 
                }
            }
            });
    })

;

Answer:2

First of all, word of explanation: I'm new to all this Meteor stuff so it might be a stupid question. In my Meteor code I have a template: <template name="hour_form"> <form id="add_hours"&...

First of all, word of explanation: I'm new to all this Meteor stuff so it might be a stupid question. In my Meteor code I have a template: <template name="hour_form"> <form id="add_hours"&...

I'm new to JavaScript server developement. I recognized the "mean" stack. MongoDB, Express.js, Angular.js and node.js. Where is the difference between express.js and anguar.js and do I need both at ...

I'm new to JavaScript server developement. I recognized the "mean" stack. MongoDB, Express.js, Angular.js and node.js. Where is the difference between express.js and anguar.js and do I need both at ...

  1. where difference between two dates sql
  2. where difference between were
  3. which difference between plant and animal cells
  4. which difference between weather and climate
  5. which difference between data and information
  6. which difference between stocks and bonds
  7. which difference between empathy and sympathy
  8. which difference between a city and a town
  9. which difference between authentication and authorization
  10. where is difference between visa and mastercard
  11. where is difference threshold
  12. where is the difference between peanut butter and jam
  13. where is the difference between college and university
  14. where is the difference between jail and prison
  15. where is the difference between mitosis and meiosis
  16. where is the difference between a psychologist and a psychiatrist
  17. where is the difference between a sociopath and a psychopath
  18. where is the difference between there and their
  19. where is the difference between vegetarian and vegan
  20. where is the difference between a typhoon and a hurricane

I have a form with a bunch of text elements, some of which have a data attribute set. I want to loop through all the elements that have that attribute, extracting the attribute. I've created a ...

I have a form with a bunch of text elements, some of which have a data attribute set. I want to loop through all the elements that have that attribute, extracting the attribute. I've created a ...

  1. find elements that sum to a given value
  2. find elements in selenium
  3. find elements in list python
  4. find elements in array matlab
  5. find elements in array javascript
  6. find elements in array python
  7. find elements in selenium python
  8. find elements in array with given sum
  9. find elements in matrix matlab
  10. find elements in numpy array
  11. find elements in vector c++
  12. find elements in cell array matlab
  13. find elements in selenium c#
  14. find elements in vector matlab
  15. find elements in appium
  16. find elements in array
  17. find elements in protractor
  18. find elements in array java
  19. find elements in page factory
  20. find elements in selenium java

I have seen lots of confusion with TimeZones and Angular Date filters, but my issue seems a little different. I am storing all dates and times in the database as UTC. The following code snippet - {...

I have seen lots of confusion with TimeZones and Angular Date filters, but my issue seems a little different. I am storing all dates and times in the database as UTC. The following code snippet - {...

  1. angularjs date filter timezone
  2. angularjs date filter format
  3. angularjs date filter not working
  4. angularjs date filter in controller
  5. angularjs date filter locale
  6. angularjs date filter utc
  7. angularjs date filter example
  8. angularjs date filter in html
  9. angularjs date filter dd/mm/yyyy
  10. angularjs date filter ignore timezone
  11. angularjs date filter mm/dd/yyyy
  12. angularjs date filter 24 hours
  13. angularjs date filter in controller example
  14. angularjs date filter timezone example
  15. angularjs date filter
  16. angularjs date filter am pm
  17. angularjs date filter w3schools
  18. angularjs date filter locale timezone
  19. angularjs date filter day of week
  20. angularjs date filter weekday