Jump to content

Array won't add, in Javascript


njaohnt

Recommended Posts

So, the beatified code:

 

<html>
<head>
<title>Time</title>
<script type="text/javascript">
function d() {
    var j = [18, 14, 37];
    var t = new Date();
    var c = [t.getHours, t.getMinutes, t.getSeconds];
    var a = [c[0] - j[0], c[1] - j[1], c[2] - j[2]];
    var b = "After Jz's";
    if (a[0] < 0) {
        a[0] = j[0] - c[0];
        b = "Before Jz's";
    }
    if (a[1] < 0) a[1] = j[1] - c[1];
    if (a[2] < 0) a[2] = j[2] - c[2];
    for (i = 0; i < 3; i++)
    document.getElementById("r" + i).innerHTML = a[0];
    document.getElementById("r").innerHTML = b;
    document.getElementById("t").innerHTML = "Your computer's date and time:" + Date() + "<br>Jz's date and time:Sunday 18 September 2011 6:14:37.1316394877.92 PM";
}
</script>
</head>
<body onload="d()">
<div id="t">
</div>
<br>
<div id="r0">
</div>
h
<div id="r1">
</div>
m

 

A lot easier to see now.

 

First problem:

 

document.getElementById("r")

 

There is no element with an ID of "r".

Link to comment
Share on other sites

  • 1 month later...

QUOTE (njaohnt @ Sep 19 2011, 08:11 PM)

It can byron.

 

 

If nobody here at helionet can show you how, try this forum.

 

http://www.codingforums.com

 

I'm sure if it can be done, somebody there will know how. If you come up with a solution, please post it back here in this thread.

 

 

No need to go elsewhere, I can help you out.

 

Your problem is this line:

var c = [t.getHours, t.getMinutes, t.getSeconds];

 

You're storing the function reference and not the result. You should call the functions (add parentheses):

var c = [t.getHours(), t.getMinutes(), t.getSeconds()];

 

It's "NaN" because you then calculate c[0]-j[0] which translates to "t.getHours"-12 (or some other number), so when you do a calcuation of a non-number string minus a number, it'll result to NaN. if you store the result of t.getHours() in c[0], the calculation would be two numbers (eg 9-8)

 

Note: I didn't do the work for you, I only removed the NaN bug, you'll have to debug your code to get it to work:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <meta name="generator" content="HTML Tidy for Linux/x86 (vers 11 February 2007), see www.w3.org" />
 <title>Time</title>
 <script type="text/javascript">
 //<![CDATA[
 function d() 
 {
   var j = [ <?php echo idate("H"); ?> , <?php echo idate("i"); ?> , <?php echo idate("s"); ?> ];
   var t = new Date();
   var c = [t.getHours(), t.getMinutes(), t.getSeconds()];
   var a = [c[0] - j[0], c[1] - j[1], c[2] - j[2]];
   var b = "After Ours";
   if (a[0] < 0) {
       a[0] = j[0] - c[0];
       b = "Before Ours";
   }
   if (a[1] < 0) a[1] = j[1] - c[1];
   if (a[2] < 0) a[2] = j[2] - c[2];
   for (i = 0; i < 3; i++)
   document.getElementById("r" + i).innerHTML = a[0];
   document.getElementById("r").innerHTML = b;
   document.getElementById("t").innerHTML = "Your computer's date and time:" + Date() + "<br>Our date and time:<?php 
     $aop=" AM "; if (idate('H')>12) $aop=" PM ";
     $dow=array("Sunday ","Monday ","Tuesday ","Wednesday ","Thursday ","Friday ","Saturday ");
     $mow=array("",'January','February','March','April','May','June','July','August','September','October','November','December');
     $min=idate("i");
     $sec=idate("s");
     if ($min<10)
     $min="0".idate("i");
     if ($sec<10)
         $sec=" 0 ".idate("s");
     $d=array($dow[idate('w')],idate('d'),$mow[idate('m')],idate('Y'),idate('h'),$min,$sec,microtime(get_as_float),$aop);
     echo $d[0]."".$d[1]."".$d[2]."".$d[3]."".$d[4].": ".$d[5].": ".$d[6].".".$d[7]."".$d[8];
?>";
 }
 //]]>
 </script>
</head>
<body onload="d()">
 <div id="t"></div><br />
 <div id="r0"></div>h
 <div id="r1"></div>m
 <div id="r2"></div>s
 <div id="r"></div><button onclick="window.location.href='servertime.php'">Update Time</button>
</body>
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...