Jump to content

First Javascript

- - - - -

  • Please log in to reply
9 replies to this topic

#1
chaser

chaser

    Newbie

  • Members
  • Pip
  • 2 posts
Hello all,
I'm brand new to web design and development and have just finished writing my first code for a website I am tinkering with. It may be overcomplicated for the purpose, but I just wanted to see what someone thought about it.

var days = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];


var months = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];


var newDate = new Date();

var today = newDate.getDate();

var lastDigit = today % 10; 

var ending;


function addTheEnding() {

	"use strict";

	if (lastDigit === 1) {

		ending = "st";

	} else {

		if (lastDigit === 2) {

			ending = "nd";

		} else {

			if (lastDigit === 3) {

				ending = "rd";

			} else {

				ending = "th";

			}

		}

	}

}


addTheEnding();


function updateDate() {

	"use strict";

	return (days[newDate.getDay()] + " " + months[newDate.getMonth()] + " " + today + ending);

}


var currentDate = document.createTextNode(updateDate());


document.getElementById("date").appendChild(currentDate);


Truth is, I'm terribly proud of this simple little bit of code. I certainly stole the lastDigit idea. I was going to use the || operator. Still, it took me surprisingly long to get this to work properly. I know it could have been done in a more direct manner, but I thought it might be a good idea to practice using the more precise methods I will need as the code gets more complicated. Any thoughts? Should I separate the addTheEnding function into a couple of functions to simplify the if nesting? Oh and what about this "use strict" deal? I have seen a little bit about it. I mean I'm new to this and if they think we should use it I don't mind learning to do it with it.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript

	if (lastDigit === 1) {
		ending = "st";
	} else {
		if (lastDigit === 2) {
			ending = "nd";
		} else {
			if (lastDigit === 3) {
				ending = "rd";
			} else {
				ending = "th";
			}
		}
	}

Can also be written as:
switch(lastDigit)
{
    case 1:
        ending = "st";
        break;

    case 2:
        ending = "nd";
        break;

    case 3:
        ending = "rd";
        break;

    default:
        ending = "th";
}

A lot easier to read.
sudo rm -rf /

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
And also, you might not even need the braces for the 'else' .

Such as, this:
if (x == 1){ 

  x= 2; 

else { 

  if (x == 2){ 

    x= 3; 

  } else { 

    // .....  

  } 

} 
is the same as this:
if (x == 1){ 

  x= 2; 

} else if (x == 2){ 

  x= 3; 

} else { 

  // .....  

} 

JavaScript doesn't need the braces if it's just one statement after the 'else' keyword.

(Though you could say:
if (x == 1) x= 2; 

else if (x == 2) x= 3; 

else /* ..... */ ; 
; I think this should work.)

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Switch statements are still more concise. Plus they allow fall-throughs, which is annoying to do with an if-else block.

switch( blah )
{
    case 1:
        do_something();
        /* fall through */
        
    case 2:
        something_else();
        break;
    
    case 3:
    case 4:
        yet_something_else();
        break;
        
    default:
        explode();
}

Here, for example, if blah is 1, it will execute both do_something()then fall through and call something_else(), but if it's 2, then it'll just execute something_else(). yet_something_else() is executed for 3 and 4.

(Please excuse the font issues, my browser is misbehaving at the moment.)

sudo rm -rf /

#5
chaser

chaser

    Newbie

  • Members
  • Pip
  • 2 posts
Thanks for the reply. Switched to a switch statement and it is much cleaner. Took me a minute to figure out that case 1, literally meant that if the value of the variable was === 1 it should execute that code block, but that makes sense. I'm sure I will use them often. Thanks again!

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
=== ... are you a PHP user? :)
sudo rm -rf /

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

Quote

===
I didn't get that either.

Isn't it supposed to be "==" ?

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
In PHP, == means "is equal to" with type coercion, i.e. 0, "", and false are considered the same. === means "is identical to" in which case 0, "", and false are not the same.
sudo rm -rf /

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I got it now. This page says some stuff about comparison operators: JavaScript Comparison and Logical Operators.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Huh. I had no idea that was also valid JavaScript. Learn something new every day.
sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users