I'm a web developer who works with HTML, PHP, CSS, ExpressionEngine, Javascript, jQuery, MySQL, SEO and UI/UX design. Always looking to learn new web related stuff, and a bit of a PC gamer. You can also find me on Twitter and LinkedIn.

Posts Tagged: javascript

Text

Here’s a little javacript function that will get the date ordinal (the bit after the date which can be st, nd, rd or th). It’s by no means perfect, and there’ most likely a more efficient way of doing it, but thought it useful enough to share

function get_date_ordinal(num) {
  var end_num = num.charAt(num.length - 1);
  var start_num = num.charAt(0);
  var abbrev = 'th';
  if (end_num == '1' && start_num != '1') abbrev = 'st';
  if (end_num == '2' && start_num != '1') abbrev = 'nd';
  if (end_num == '3' && start_num != '1') abbrev = 'rd';
  return num + abbrev;
}

I knocked this together as I was pulling in a daily RSS feed, and wanted to present the date in a slightly different way to what was provided.

Text

Comment your code.

It doesn’t really matter what code you’re writing, whether it’s HTML, PHP, CSS, Javascript or anything else, commenting your code can become very important.

There are a number of reasons why we should look to comment our code;

  1. If someone else has to work with your code, it will help them decipher what the hell is going on.
  2. It can be used to search for a specific block of code by adding key phrases/symbols to search for in the comments.
  3. If you haven’t worked with a piece of code for a long time, it can refresh your memory, saving you having to work it all out again.
  4. Other people can learn from what you’ve done.

Number 4 on that list is my favourite. I’ve learnt a great many things by seeing something I like on the web, checking out the code and having a go at building it myself. Help out your fellow developers, let them learn from your work.