Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [Javascript] Is this even remotely correct? (https://thetfp.com/tfp/tilted-technology/98735-javascript-even-remotely-correct.html)

TheProf 12-13-2005 08:49 PM

[Javascript] Is this even remotely correct?
 
Hello.

I am trying to get a website to display a specific thing based on the hour of the day. I *think* this works (I'm a C person myself) but when I drop it into the CMS I'm using it displays nothing.

Thoughts?

Code:

<script Language="JavaScript">
 
var today = new Date();
var hours = today.getHours();

var readings = 0;

switch (hours)
{
case 0:
readings = 7;
  break
case 1:
readings = 7;
  break
case 2:
readings = 7;
  break
case 3:
readings = 7;
  break
case 4:
readings = 7;
  break
case 5:
readings = 7;
  break
case 6:
readings = 1;
  break
case 7:
readings = 1;
  break
case 8:
readings = 1;
  break
case 9:
readings = 2;
  break
case 10:
readings = 2;
  break
case 11:
readings = 2;
  break
case 12:
readings = 3;
  break
case 13:
readings = 3;
  break
case 14:
readings = 3;
  break
case 15:
readings = 4;
  break
case 16:
readings = 4;
  break
case 17:
readings = 4;
  break
case 18:
readings = 5;
  break
case 19:
readings = 5;
  break
case 20:
readings = 5;
  break
case 21:
readings = 6;
  break
case 22:
readings = 6;
  break
case 23:
readings = 6;
  break
}

AgpeyaHours = new Array(8);
 
AgpeyaHours[1]="Prime"
AgpeyaHours[2]="Terce"
AgpeyaHours[3]="Sext"
AgpeyaHours[4]="None"
AgpeyaHours[5]="Vespers"
AgpeyaHours[6]="Compline"
AgpeyaHours[7]="Midnight"

document.write(AgpeyaHours[readings]);
</script>


feelgood 12-13-2005 11:13 PM

I think "break" is suppose to have ; at the end

I'm drunk btw, don't blame me if its the way its suppose to be

Jinn 12-13-2005 11:43 PM

You don't need ; on breaks, in Javascript at least.

This code seems to run fine on my box; I just make a blank html and inserted the Script. This sounds like an issue with your CMS or how this jscript is integrated with your other code (if any).

That said, wouldn't it be simpler to write it this way? It seems a bit clunky to make an switch statement for 24 cases when you only have a few options;

Code:

<script language="JavaScript">

var today = new Date();
var hours = today.getHours();

if (hours >= 0 && hours < 6)
        document.write("Midnight");

else if (hours >= 6 && hours <9)
        document.write("Prime");

else if (hours >= 9 && hours <12)
        document.write("Terce");

else if (hours >= 12 && hours < 15)
        document.write("Sext");

else if (hours >= 15 && hours < 18)
        document.write("None");

else if (hours >= 18 && hours < 21)
        document.write("Vespers");

else
        document.write("Compline");

</script>

Just a suggestion.. this will even work if you decide to expand to include the different prayers for each time.. just change the document.write string.

TheProf 12-14-2005 07:51 AM

Quote:

Originally Posted by JinnKai
You don't need ; on breaks, in Javascript at least.

This code seems to run fine on my box; I just make a blank html and inserted the Script. This sounds like an issue with your CMS or how this jscript is integrated with your other code (if any).

I came to that conclusion by doing the same thing. It seems to be a problem with how the CMS is running the script.
Quote:

Originally Posted by JinnKai
That said, wouldn't it be simpler to write it this way? It seems a bit clunky to make an switch statement for 24 cases when you only have a few options;
<snipped great code>
Just a suggestion.. this will even work if you decide to expand to include the different prayers for each time.. just change the document.write string.

Thank you very much! That is far more elegant code than the brute force one I had.

Now if only I can get it to work in the CMS :)

thanks again


All times are GMT -8. The time now is 12:13 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76