Skip to content

Latest commit

 

History

History
106 lines (88 loc) · 3.41 KB

File metadata and controls

106 lines (88 loc) · 3.41 KB

Algorithm - Date and String

table of contents

  1. Show tomorrow date
  2. Define Date.nextDay using prototype
  3. Get URL
  4. Get a domain name
  5. Get a path name
  6. Get a file name
  7. Get domain and pathname using regular expression
  8. Differences between each() and forEach().

Show tomorrow date

var today = new Date();
var tomorrow = new Date();
tomorrow.setDate(today.getDate()+1);

console.log(today);//Thu Dec 08 2016 08:35:42 GMT-0800 (PST)
console.log(tomorrow); //Fri Dec 09 2016 08:32:48 GMT-0800 (PST)

Get URL.

window.location.href;
"https://github.com/hirokoymj/Algorithm_Date/edit/master/README.md"

Get domain name.

window.location.host;
"github.com"

Get a path name.

var pathname = window.location.pathname;
"/hirokoymj/Algorithm_Date/edit/master/README.md"

Get a file name from url.

var url = "https://github.com/hirokoymj/Algorithm_Date/edit/master/README.md";
var filename = url.substring(url.lastIndexOf('/')+1);
console.log(filename); //README.md

Get domain name, path name and file name using Regular expression

  1. Creating a regular expression object. var reg = new RegExp()
  2. ? means non-greedy.
  3. () is grouping and can access using $n.
  4. Use string.replace() method.
  5. Use string.substring() method.
  6. Use string.lastIndexOf() method.
var url = "https://github.com/hirokoymj/Algorithm_Date/edit/master/README.md";
var reg = new RegExp('https://(.+?)/(.+)');
var host = url.replace(reg, '$1');
var pathname = url.replace(reg, '$2');
var filename = url.substring(url.lastIndexOf('/')+1);

console.log(host); 		//github.com
console.log(pathname); //hirokoymj/Algorithm_Date/edit/master/README.md
console.log(filename); //README.md

Differences between forEach() and each().

  1. forEach() is JavaScript function to iterate for Array. - Array.forEach().
  2. each() is jQuery function to iterate HTML.

forEach

var employees = [
  { skill: 'css', user: 'Bill' },
  { skill: 'javascript', user: 'Chad' },
  { skill: 'javascript', user: 'Bill' },
  { skill: 'css', user: 'Sue' },
  { skill: 'javascript', user: 'Sue' },
  { skill: 'html', user: 'Sue' }
];
employees.forEach(function(value, index){
	console.log(value + ',' + index);
})

$.each()

$('li a').each(function(value){
  console.log($(this).attr('href');
});

References: