Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2012, 03:03 AM   PM User | #1
Colombia
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Colombia is an unknown quantity at this point
array prints incorrect values

I initialize part of an array, but when I display them, they are incorrect or duplicated.

Here is my code:

Code:
<html>
<head>
<title> Message display based on day and time </title>
<script>
function getdhh()
{
var prog = new Array(623);

  
// Fill array with 168 messages, one each hour of week,

prog[000] = "Sunday 00:00";
prog[001] = "Sunday 01:00";
prog[002] = "Sunday 02:00";
prog[003] = "Sunday 03:00";
prog[004] = "Sunday 04:00";
prog[005] = "Sunday 05:00";
prog[006] = "Sunday 06:00";
prog[007] = "Sunday 07:00";
prog[008] = "Sunday 08:00";
prog[009] = "Sunday 09:00";
prog[010] = "Sunday 10:00";
prog[011] = "Sunday 11:00";
prog[012] = "Sunday 12:00";
prog[013] = "Sunday 13:00";
prog[014] = "Sunday 14:00";
prog[015] = "Sunday 15:00";
prog[016] = "Sunday 16:00";
prog[017] = "Sunday 17:00";
prog[018] = "Sunday 18:00";
prog[019] = "Sunday 19:00";
prog[020] = "Sunday 20:00";
prog[021] = "Sunday 21:00";
prog[022] = "Sunday 22:00";
prog[023] = "Sunday 23:00";
prog[025] = "."
  

prog[101] = "Monday 01:00";
prog[111] = "Monday 11:00";
prog[122] = "Monday 22:00";

prog[202] = "Tues. 02:00";
prog[203] = "Tues. 03:00";
prog[209] = "Tues. 09:00";

prog[310] = "Wed. 10:00";
prog[320] = "Wed. 20:00";
prog[323] = "Wed. 23:00";

prog[400] = "Thursday 00:00";
prog[401] = "Thursday 01:00";
prog[411] = "Thursday 11:00";
prog[421] = "Thursday 21:00";

prog[500] = "Friday 00:00";
prog[512] = "Friday 12:00";

prog[600] = "Sat. 00:00";
prog[623] = "Sat. 23:00";

  

document.write("<br> prog(000) is ");
document.write(prog[000]);
document.write("<br> prog(001) is ");
document.write(prog[001]);
document.write("<br> prog(002) is ");
document.write(prog[002]);
document.write("<br> prog(003) is ");
document.write(prog[003]);
document.write("<br> prog(004) is ");
document.write(prog[004]);
document.write("<br> prog(005) is ");
document.write(prog[005]);
document.write("<br> prog(006) is ");
document.write(prog[006]);
document.write("<br> prog(007) is ");
document.write(prog[007]);
document.write("<br> prog(008) is ");
document.write(prog[008]);
document.write("<br> prog(009) is ");
document.write(prog[009]);
document.write("<br> prog(010) is ");
document.write(prog[010]);
document.write("<br> prog(011) is ");
document.write(prog[011]);
document.write("<br> prog(012) is ");
document.write(prog[012]);
document.write("<br> prog(013) is ");
document.write(prog[013]);
document.write("<br> prog(014) is ");
document.write(prog[014]);
document.write("<br> prog(015) is ");
document.write(prog[015]);
document.write("<br> prog(016) is ");
document.write(prog[016]);
document.write("<br> prog(017) is ");
document.write(prog[017]);
document.write("<br> prog(018) is ");
document.write(prog[018]);
document.write("<br> prog(019) is ");
document.write(prog[019]);
document.write("<br> prog(020) is ");
document.write(prog[020]);
document.write("<br> prog(021) is ");
document.write(prog[021]);
document.write("<br> prog(022) is ");
document.write(prog[022]);
document.write("<br> prog(023) is ");
document.write(prog[023]);
document.write("<br> prog(024) is ");
document.write(prog[024]);
document.write("<br> prog(025) is ");
document.write(prog[025]);


}
</script>
</head>

<body>
<script>
getdhh();
</script>
</body>
</html>
But here is the displayed output:

Code:
prog(000) is Sunday 00:00
prog(001) is Sunday 01:00
prog(002) is Sunday 02:00
prog(003) is Sunday 03:00
prog(004) is Sunday 04:00
prog(005) is Sunday 05:00
prog(006) is Sunday 06:00
prog(007) is Sunday 07:00
prog(008) is Sunday 10:00
prog(009) is Sunday 11:00
prog(010) is Sunday 10:00
prog(011) is Sunday 11:00
prog(012) is Sunday 12:00
prog(013) is Sunday 13:00
prog(014) is Sunday 14:00
prog(015) is Sunday 15:00
prog(016) is Sunday 16:00
prog(017) is Sunday 17:00
prog(018) is Sunday 22:00
prog(019) is Sunday 23:00
prog(020) is Sunday 20:00
prog(021) is Sunday 21:00
prog(022) is Sunday 22:00
prog(023) is Sunday 23:00
prog(024) is undefined
prog(025) is .
Notice how some of the values are duplicated. This is my first attempt at writing javascript, so I could be doing something wrong; but I don't know what.
Colombia is offline   Reply With Quote
Old 10-08-2012, 03:38 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
just a question before I actually look at your code... is that all that you want to do? Because javascript has a date object that makes these kinds of things quite simple...

Code:
<html>
<body>
<div id="results">
</div>
<script>
var days=["Sunday","Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday"]
var d=new Date(2012,9,7,0,0,0)
for (var i = 0; i < 168; i++) {
if(i<10){var pre="00"}
else if(i>9&&i<100){var pre="0"}
else{var pre=""}
document.getElementById("results").innerHTML+="prog ("+pre+i+") is "+days[d.getDay()]+" "+d.toTimeString().substr(0,5)+"<br>"
d.setHours(d.getHours()+1);
}
</script>
</body>
</html>

Last edited by xelawho; 10-08-2012 at 04:01 AM.. Reason: correcting silliness
xelawho is offline   Reply With Quote
Old 10-08-2012, 05:05 AM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
... but the answer to your original question is that you shouldn't use leading zeros for numbers, as javascript can convert them to octal numbers. Have a look:

Code:
<script>
var octs=[010,011,012,013,014];
for (var i = 0; i < octs.length; i++) {
alert(octs[i]);
}
</script>
if you were going to make an array like that, do it so:
Code:
prog[0] = "Sunday 00:00";
prog[1] = "Sunday 01:00";
prog[2] = "Sunday 02:00";
prog[3] = "Sunday 03:00";
//etc
xelawho is offline   Reply With Quote
The Following 2 Users Say Thank You to xelawho For This Useful Post:
anirbanguha (10-11-2012), Colombia (10-09-2012)
Old 10-09-2012, 02:47 AM   PM User | #4
Colombia
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Colombia is an unknown quantity at this point
Thanks, that was the problem. Is it documented somewhere that leading zeros should not be used?

The purpose of this exercise is to fill the array with radio program titles (one per hour) so that when someone logs onto our radio station website, it will display "Now playing: The Tom Collins Show ... Click here to listen". I will have to update the script every time the schedule changes, but that's OK. I'll also need to find out how to get the hour of the day for the Eastern time zone (including Daylight times) as well as the day of the week for the Eastern time zone. If I concatenate the day of week with the hour of the day, that should point to the element in the array to display, such as Monday at 1 pm, display message 113 (1 and 13).
Colombia is offline   Reply With Quote
Old 10-09-2012, 03:49 AM   PM User | #5
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
it's not that they shouldn't be used, just that if they are used, it will be assumed that you want to use octals:
https://developer.mozilla.org/en-US/...,_and_literals

one thing you might want to be aware of is that the javascript date object will give you the date according to the user's computer, which can be set as far back as 1980 and as far forward as 2099, and anywhere in between (or at least that's the case with mine).

So it is better to get the time from the server, which you know should be accurate.

It's quite likely that there is a better way to organise your data than a simple array. Depending on how the schedule looks, it may be better to put it into objects, which would look something like this:

Code:
<body>
<div id="showing"></div>
<script type="text/javascript">
var shows={Monday:{0:"graveyard shift",
		2:"hard rock",
		4:"easy listenging",
		6:"breakfast"
		},
	Tuesday:{0:"hillbilly",
		2:"trance",
		4:"light jazz",
		6:"comedy"
		}
	}
var theday="Monday";
var thehour=2;			
document.getElementById("showing").innerHTML="Now playing "+shows[theday][thehour]+"... click <a href='http://www.radio.com'>here</a> to listen";
</script>
</body>
... all you have to do is figure a reliable way to populate the day and hour variables.

you should also search the forum (and the net at large) for existing examples - plenty of people before you have done similar things - it may not be worth reinventing the wheel
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
Colombia (10-09-2012)
Old 10-09-2012, 11:21 AM   PM User | #6
Colombia
New to the CF scene

 
Join Date: Oct 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Colombia is an unknown quantity at this point
Thanks for the suggestions; I'll try to implement them ...
Colombia is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:16 PM.


Advertisement
Log in to turn off these ads.