ALEX
ALEX

Reputation: 64

Change variable's value if the same variable is equal to a string

I am working on a small project where I have to display the current date. I need to add nth, st, and so on depending on the last letter of the variable's string, but when I check the HTML file in a web browser, the string doesn't show up. Here is my code:

window.onload = function() {
   startDate();
}

function startDate() {
   var month = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
   ];

   var weekDay = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
   ];

   var date = today.getDate();

   date = checkDate(date);

   document.getElementById("date").innerHTML = weekDay[today.getDay()] + " the " + date + " of " + month[today.getMonth()];
}

function checkDate(i) {
   if (i.slice(-1) == 1 && i !== 11) {
      i = i + "st";
   } else if (i.slice(-1) == 2 && i !== 12) {
      i = i + "nd";
   } else if (i.slice(-1) == 3 && i !== 13) {
      i = i + "rd";
   } else {
      i = i + "th";
   }

   return i;
}

Here's my HTML:

<!DOCTYPE html>
<html>
<head>
   <meta charset="utf-8">
   <link rel="stylesheet" href="style.css" />
</head>
<body>
   <div id="container">
      <div id="date"></div>
   </div>
   <script src="script.js"></script>
</body>
</html>

Upvotes: 0

Views: 584

Answers (2)

trincot
trincot

Reputation: 350770

You need to define today before using it, as follows:

var today = new Date();
var date = today.getDate();

The function checkDate receives a numeric argument, but then treats it as a string by using slice: this produces an error. You should treat it as a number, for instance with i % 10:

function checkDate(i) {
   return (i % 10 == 1 && i !== 11) ? i + "st"
        : (i % 10 == 2 && i !== 12) ? i + "nd"
        : (i % 10 == 3 && i !== 13) ? i + "rd"
        : i + "th";
}

Upvotes: 0

Mikhail Chibel
Mikhail Chibel

Reputation: 1955

You have some issues with your code. Firstly how are you define 'today'?. Secondly, the slice function is not defined on Number object. With these two edits your working code:

window.onload = function() {
   startDate();
};
function startDate() {
   var month = [
      "January",
      "February",
      "March",
      "April",
      "May",
      "June",
      "July",
      "August",
      "September",
      "October",
      "November",
      "December"
   ];

   var weekDay = [
      "Sunday",
      "Monday",
      "Tuesday",
      "Wednesday",
      "Thursday",
      "Friday",
      "Saturday"
   ];
  var today = new Date();

   var date = today.getDate();

   date = checkDate(String(date));

   document.getElementById("date").innerHTML = weekDay[today.getDay()] + " the " + date + " of " + month[today.getMonth()];
}

function checkDate(i) {
   if (i.slice(-1) == 1 && i !== 11) {
      i = i + "st";
   } else if (i.slice(-1) == 2 && i !== 12) {
      i = i + "nd";
   } else if (i.slice(-1) == 3 && i !== 13) {
      i = i + "rd";
   } else {
      i = i + "th";
   }

   return i;
}

Upvotes: 1

Related Questions