SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript issue

    Imagine i have a table with these values:

    Date(Column) Hours(Column)
    01.07.2010 9
    03.07.2010 9

    In this table you would have noticed that there are Hours value available for dates 01 and 03. But for 02 it is not there. How will i use javascript and get to know the date that is not present in this table. I need to know the missing date. can anyone help me with the logic?

  2. #2
    SitePoint Zealot blackman890's Avatar
    Join Date
    Feb 2005
    Location
    Iceland
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure Javascript is the best solution and not do it server-side in PHP/ASP/Python/and so on?
    Jonatan Nilsson
    Iceland
    C# Programmer - XML & DirectX (y = hx + c)

  3. #3
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im supposed to do it in javascript only. As it is a tool where u have to use javascript to do some functionalities. do you know the solution to solve this problem?

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Initialise a counter variable to 0.
    Loop over each table row.
    For each row, extract the date part from the first cell.
    Increment the counter variable.
    While the date value > the counter variable, add the counter value to an array and increment the variable.

    Afterwards you'll have an array containing the dates that aren't present in the table.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AutisticCuckoo. Could u please write this code and post it here. I am a beginner in javascript so I am not sure how to implement it codewise. Hoping to hear from you soon

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You asked for the logic, not the details, but okay.

    Assuming the following table,
    Code HTML4Strict:
    <table>
      <thead>
        <tr>
          <th>Date</th>
          <th>Hours</th>
        </tr>
      </thead>
      <tbody id="hours">
        <tr>
          <td>01.07.2010</td>
          <td>9</td>
        </tr>
        <tr>
          <td>03.07.2010</td>
          <td>9</td>
        </tr>
      </tbody>
    </table>
    this code would show the missing date(s):
    Code JavaScript:
    var rows = document.getElementById("hours").rows, day = 0, missing = [], d;
    for (var i = 0; i < rows.length; ++i) {
        d = parseInt(/^(\d+)/.exec(rows[i].cells[0].firstChild.data)[1], 10);
        while (d > ++day) {
            missing.push(day);
        }
    }
    alert("Missing days: " + missing.join(", "));
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Member
    Join Date
    Jun 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This logic is fine. But in the table if we have 2 entries or 2 hour values for the same date. Then the count incrementation wont work fine.

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sanilsunny View Post
    This logic is fine. But in the table if we have 2 entries or 2 hour values for the same date. Then the count incrementation wont work fine.
    You didn't tell me the same date could occur more than once!

    Code JavaScript:
    var rows = document.getElementById("hours").rows, day = 0, missing = [], d;
    for (var i = 0; i < rows.length; ++i) {
        d = parseInt(/^(\d+)/.exec(rows[i].cells[0].firstChild.data)[1], 10);
        while (d > ++day) {
            missing.push(day);
        }
        day = d;
    }
    alert("Missing days: " + missing.join(", "));
    Birnam wood is come to Dunsinane


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •