0

I am trying to display the current date and a name of a famous person who was born on the same date. I cannot get the name of the famous person to show up. Any ideas why? I would appreciate any help!

Here is the HTML file:

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC " //W3C//DTD XHTML1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<!--
    Tutorial 10
    Case Problem 4

    Author: Collin Klopstein
    Date: December 5, 2013

    This Web Page is created to display the current day's date and give a name of a famous person who is born on the same day.
-->

    <title>Happy Birthday</title>
    <link type="text/css" rel="stylesheet" href="date.css" />

    <script type="text/javascript" src="functions.js"></script>

</head>
<body>
    <div id="links">
        <ul>
            <li><a href="index.htm">Home</a></li>
            <li><a href="birthday.htm">Happy Birthday</a></li>
            <li><a href="dinnerplate.htm">Dinner Plate</a></li>
            <li><a href="tempest.htm">Shakespeare</a></li>
        <ul>
    </div>
    <div id="pic">
        <img src="logo.jpg" alt="happy birthday" />
    </div>
    <div id="star">
        <script type="text/javascript">
            document.write("<p>Today's Date is</p>");
            document.write(showDate());
            document.write("<p>A star born today</p>");
            document.write(showBirthDay(dayNumber()));
        </script>
    </div>

</body>
</html>

Here is the JavaScript File:

/*
   New Perspectives on HTML and XHTML 5th Edition
   Tutorial 10
   Case Problem 4

   Function List:
   showDate
      Used to display the date in the form "Weekday, Month Day, Year"

   dayNumber
      Used to calculate the day number (1 - 366) of a given date

   showBirthDay
      Used to display a famous birthday falling on a given date
*/

var births = new Array();
   births[1] = "J.D. Salinger (1919) - Author";
   births[2] = "Isaac Asimov (1920) - Author";

   ... // lots of birthdays here

   births[364] = "Jude Law (1972) - Actor";
   births[365] = "Rudyard Kipling (1865) - Author";
   births[366] = "Bonnie Prince Charlie (1720) - Attempted to seize England";


function showDate() {
   thisDate = new Date();
   var thisWDay=thisDate.getDay();
   var thisDay=thisDate.getDate();
   var thisMonth=thisDate.getMonth();
   var thisYear=thisDate.getFullYear();
   var mName = new Array("January", "February", "March", "April", "May", 
       "June", "July", "August", "September", "October","November", "December");
   var wdName = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
       "Thursday", "Friday", "Saturday");
   return wdName[thisWDay]+", "+mName[thisMonth]+" "+thisDay+", "+thisYear;
}

function dayNumber() {
   thisDate = new Date();
   var leapYearDate = thisDate.setFullYear(2004);
   var baseDate = new Date("January 1, 2004");
   days = Math.floor((leapYearDate - baseDate)/(1000*60*60*24)+1);
   return days;
}

function showBirthDay(day) {
        return births[day];
}
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072
howdydoody
  • 93
  • 2
  • 17

1 Answers1

0

document.write() does not work with XHTML.

http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

StackSlave
  • 10,198
  • 2
  • 15
  • 30
  • 2
    Values used as property names always coerced to strings. – Felix Kling Dec 08 '13 at 22:32
  • `day` isn't a string. `dayNumber()` returns a number. – JJJ Dec 08 '13 at 22:33
  • @Felix Kling : nope : to strings for Objects, and to numbers for arrays (toString vs valueOf). if the conversion to number fails, the string is used and the Array Object is used only as an Object for this property access. – GameAlchemist Dec 09 '13 at 00:18
  • Yes: *"The production `MemberExpression : MemberExpression [ Expression ]` is evaluated as follows: [...] 6. Let `propertyNameString` be `ToString(propertyNameValue)`."* http://es5.github.io/#x11.2.1 – Felix Kling Dec 09 '13 at 00:24
  • 1
    Btw, it looks like you added ` – Felix Kling Dec 09 '13 at 00:28
  • ok, any use of the bracket notation will convert te expression to string. And since integers are required for an array, they will *also* be converted to integer at some point. That explains why arrays are slower than objects -a well known fact-. – GameAlchemist Dec 09 '13 at 11:48