help coding a hash table #2

J

JRough

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.
http://www.mojavelinux.com/articles/javascript_hashes.html
Thanks,
 
M

Michael Haufe (TNO)

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.http://www.mojavelinux.com/articles/javascript_hashes.html
Thanks,


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?
 
B

Bart Van der Donck

JRough said:
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.http://www.mojavelinux.com/articles/javascript_hashes.html


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
context.

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,
 
J

John G Harris

On Mon, 6 Feb 2012 at 20:07:13, in comp.lang.javascript, JRough wrote:

I read the page on Hash TAbles and it isn't clear.
http://www.mojavelinux.com/articles/javascript_hashes.html

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 = {};
o.red = 5;
o.blue= 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
 
D

Dr J R Stockton

In comp.lang.javascript message <efc2aeda-f605-40b2-979c-ed5972313e55@t2
4g2000yqj.googlegroups.com>, Tue, 7 Feb 2012 02:24:34, Bart Van der
Donck said:
Looping through every second is not recommended, as you will end up
with 86,400 loops.

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.
 
B

Bart Van der Donck

Dr said:
In comp.lang.javascript message <efc2aeda-f605-40b2-979c-ed5972313e55@t2
4g2000yqj.googlegroups.com>, 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.

It depends what happens inside the loop. The problem occured only
while document.write-ing the results (00:00:00-23:59:59).
 
J

JRough

JRough said:
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.http://www.mojavelinux.com/articles/javascript_hashes.html


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
context.

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,


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. http://stephenwoods.net/demos/timepicker/demo.html 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.
http://stephenwoods.net/demos/timepicker/demo.html
 
J

Jeff North

On Thu, 9 Feb 2012 18:04:40 -0800 (PST), in comp.lang.javascript
JRough <[email protected]>
<79930e44-4870-4d76-93c1-874f13d8d8ec@sk8g2000pbc.googlegroups.com>
wrote:

[snip]
|
| 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. http://stephenwoods.net/demos/timepicker/demo.html 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.
| http://stephenwoods.net/demos/timepicker/demo.html

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">
 
E

Evertjan.

Jeff North wrote on 10 feb 2012 in comp.lang.javascript:
<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>

What about invisible radio input and labels:

<http://hannivoort.org/test/invisibleRadio.asp>

Sorry,
quick and dirty, but very quick,
no am/pm-crap and no prevention of impossible numbers.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top