help coding a hash table #2

Discussion in 'Javascript' started by JRough, Feb 7, 2012.

  1. JRough

    JRough Guest

    This is the time array, with hour key value pair.
    var time = new Array();
    time['hour'][0] = "12:00";
    time['hour'][1] = "13:00";
    time['hour'][2] = "14:00";
    time['hour'][3] = "15:00";
    time['hour'][4] = "16:00";
    time['hour'][5] = "17:00";
    time['hour'][6] = "18:00";
    time['hour'][7] = "19:00";
    time['hour'][8] = "20:00";
    time['hour'][9] = "21:00";
    time['hour'][10] = "22:00";
    time['hour'][11] = "23:00";
    time['hour'][12] = "24:00";
    time['hour'][13] = "1:00";
    time['hour'][14] = "2:00";
    time['hour'][15] = "3:00";
    time['hour'][16] = "4:00";
    time['hour'][17] = "5:00";
    time['hour'][18] = "6:00";
    time['hour'][19] = "7:00";
    time['hour'][20] = "8:00";
    time['hour'][21] = "9:00";
    time['hour'][22] = "10:00";
    time['hour'][23] = "11:00";
    // show the values stored
    for (var i in time[hour]) {
    { }

    I think there is another loop inside the for in that adds the
    minutes [00,01,02,03,04....60]
    and also another one for the seconds? If you could give me an
    example. I read the page on Hash TAbles and it isn't clear.
    JRough, Feb 7, 2012
    1. Advertisements

  2. What are you trying to do in the bigger picture? What is your goal? Is
    this something you made to teach yourself or are you trying to
    accomplish something else?
    Michael Haufe (TNO), Feb 7, 2012
    1. Advertisements

  3. These assignments are invalid; first you declare 'time' as an Array,
    but then you handle it as an Object with one key ('hour').

    The correct initialization would be:

    var time = new Object();
    time.hour = new Array();

    But it's better to not use the words 'time' and 'hour' in this

    The following code would give you hours, minutes, seconds:

    // hours
    for (var i=0; i<=23; ++i) document.write(i+':00<br>');
    // minutes or seconds
    for (var i=0; i<=60; ++i) document.write(i+':00<br>');

    Looping through every second is not recommended, as you will end up
    with 86,400 loops.

    To store it in your data format:

    // init
    var t = new Object();
    t.h = new Array();
    t.m = new Array();
    t.s = new Array();

    // hours
    for (var i=0; i<=23; ++i) {
    t.h = i + ':00';

    // minutes or seconds
    for (var i=0; i<=60; ++i) {
    t.m = i;
    t.s = i;

    But I don't see why it's useful; because you then have t.h[11] =
    '11:00', t.m[48] = 48, t.s[23] = 23, etc.

    Hope this helps,
    Bart Van der Donck, Feb 7, 2012
  4. On Mon, 6 Feb 2012 at 20:07:13, in comp.lang.javascript, JRough wrote:

    That article completely misunderstands arrays and objects. An Array
    object is an ordinary object with the extra feature that its properties
    can have numeric values, as in a[3], and a length property that is one
    more than the highest numeric index in use.

    If you want to store string:value couples use an ordinary object, as in
    var o = {}; = 5; 9;
    o["white"] = 3; /* alternative "array" notation */

    And another thing, a 'hash' table is a particular kind of storage
    structure; it is unlikely to be used to implement objects (an empty
    object would grab a kB of ram or so).

    John G Harris, Feb 7, 2012
  5. In comp.lang.javascript message <[email protected]>, Tue, 7 Feb 2012 02:24:34, Bart Van der
    Is that a problem? I have Zeller's 1886 algorithm for Gregorian Easter
    Sunday, and can do a million values in 222 ms. And I can push a million
    numbers into an array in 106 ms. Firefox 10, Win XP sp3, P4/3GHz.
    Dr J R Stockton, Feb 8, 2012
  6. It depends what happens inside the loop. The problem occured only
    while document.write-ing the results (00:00:00-23:59:59).
    Bart Van der Donck, Feb 9, 2012
  7. JRough

    JRough Guest

    Many thanks for all the replies and explaining how to initialize the
    array. What the bigger picture is I want a time picker to facilitate
    user input of time [hours,minutes,seconds] something like YUI 3
    timepicker. I
    want 3 related drop downs in a input field. I want the user to select
    the hour
    with a click event. Then that triggers another event that relates a
    dropdown with the minutes. They click the minutes and it triggers
    another event to select the minutes. So after thinking about it I
    only need 1 array from 1 to 60. I can use that array 3 times. Or
    to make it easier I can use 2 arrays. 1 for hours and 1 for minutes
    and seconds. I need the loop for the front end, which I guess would
    be your for loops tied to the 3 events.
    JRough, Feb 10, 2012
  8. JRough

    Jeff North Guest

    On Thu, 9 Feb 2012 18:04:40 -0800 (PST), in comp.lang.javascript
    JRough <>

    Why complicate things? If you want select lists they already come with
    events, namely the onChange event.
    <select name=hrs><option>1</option><option>2</option> etc etc
    <select name=mns><option>00</option><option>01</option> etc etc
    <select name=sec><option>00</option><option>01</option> etc etc
    <select name=ind><option>am</option><option>pm</option>

    If you want to force the focus onto the next select list then you
    would need to include:
    <select name=hrs onChange="do your stuff here">
    Jeff North, Feb 10, 2012
  9. JRough

    Evertjan. Guest

    Jeff North wrote on 10 feb 2012 in comp.lang.javascript:
    What about invisible radio input and labels:


    quick and dirty, but very quick,
    no am/pm-crap and no prevention of impossible numbers.
    Evertjan., Feb 10, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.