728x90
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
function printListReverseLoop(list) {
let cur = list;
const arr = [];
while(cur) {
arr.push(cur.value);
cur = cur.next;
}
for(let i = arr.length - 1; i >= 0; i--) {
console.log(arr[i]);
}
}
function printListReverseRecursion(list) {
if (list.next) {
printListReverseRecursion(list.next);
}
console.log(list.value);
}
printListReverseLoop(list);
printListReverseRecursion(list);
728x90
// loop
function printListLoop(list) {
let cur = list;
while(cur) {
console.log(cur.value);
cur = cur.next;
}
}
// recursion
function printListRecursion(list) {
console.log(list.value);
if (list.next) {
printListRecursion(list.next);
}
}
const list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null
}
}
}
};
console.log('Loop');
printListLoop(list);
console.log('Recursion');
printListRecursion(list);
728x90
function fib(n) {
if (n <= 1) {
return n;
}
return fib(n - 1) + fib(n -2);
// or
// return n <= 1 ? n : fib(n - 1) + fib(n - 2);
}
console.log(fib(3)); // 2
console.log(fib(7)); // 13
// console.log(fib(77)); // 5527939700884757(extremely slow!)
function cacheFib(n) {
let a = 1;
let b = 1;
for(let i = 3; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
return b;
}
console.log(cacheFib(3)); // 2
console.log(cacheFib(7)); // 13
console.log(cacheFib(77)); // 5527939700884757(fast)
728x90
function factorial(n) {
if ( n === 1 ) {
return 1;
}
return n * factorial(n - 1);
// or
// return n === 1 ? 1 : n * factorial(n - 1);
// or
// return n !== 1 ? n * factorial(n - 1) : 1;
// or
// return n ? n * factorial(n - 1) : 1;
}
console.log( factorial(5) ); // 120
728x90
function sumToFromLoop(n) {
let sum = 0;
for(let i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
function sumToRecursion(n) {
if (n === 1) {
return n;
}
return n + sumToRecursion(n - 1);
}
function sumToFormula(n) {
return n * (n + 1) / 2; // 👍🏻
}
console.log( sumToFromLoop(100) ); // 5050
console.log( sumToRecursion(100) ); // 5050
console.log( sumToFormula(100) ); // 5050
728x90
let room = {
number: 23
};
let meetup = {
title: "Conference",
occupiedBy: [{name: "John"}, {name: "Alice"}],
place: room
};
// circular references
room.occupiedBy = meetup;
meetup.self = meetup;
console.log( JSON.stringify(meetup, function replacer(key, value) {
return (key !== '' && value === meetup) ? undefined : value;
}));
/* result should be:
{
"title":"Conference",
"occupiedBy":[{"name":"John"},{"name":"Alice"}],
"place":{"number":23}
}
*/
728x90
let user = {
name: "Blue Park",
age: 35
};
console.log(JSON.parse(JSON.stringify(user)));
const jsonUser = JSON.stringify(user);
const backToUserObj = JSON.parse(jsonUser);
console.log('jsonUser', jsonUser);
console.log('backToUserObj', backToUserObj);

 

728x90
function formatDateVerOne(date) {
const now = new Date();
const diff = Math.round((now - date) / 1000);
if (diff <= 1) {
return 'right now';
} else if (diff <= 60) {
return `${diff} sec. ago`;
} else if (diff <= 3600) {
return `${Math.round(diff / 60)} min. ago`;
} else {
return new Date(date);
}
}
console.log( formatDateVerOne(new Date(new Date - 1)) ); // "right now"
console.log( formatDateVerOne(new Date(new Date - 30 * 1000)) ); // "30 sec. ago"
console.log( formatDateVerOne(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago"
// yesterday's date like 31.12.16 20:00
console.log( formatDateVerOne(new Date(new Date - 86400 * 1000)) );
// Solution 2
function formatDateVerTwo(date) {
let diff = new Date() - date; // the difference in milliseconds
if (diff < 1000) { // less than 1 second
return 'right now';
}
let sec = Math.floor(diff / 1000); // convert diff to seconds
if (sec < 60) {
return sec + ' sec. ago';
}
let min = Math.floor(diff / 60000); // convert diff to minutes
if (min < 60) {
return min + ' min. ago';
}
// format the date
// add leading zeroes to single-digit day/month/hours/minutes
let d = date;
d = [
'0' + d.getDate(),
'0' + (d.getMonth() + 1),
'' + d.getFullYear(),
'0' + d.getHours(),
'0' + d.getMinutes()
].map(component => component.slice(-2)); // take last 2 digits of every component
// join the components into date
return d.slice(0, 3).join('.') + ' ' + d.slice(3).join(':');
}
console.log( formatDateVerTwo(new Date(new Date - 1)) ); // "right now"
console.log( formatDateVerTwo(new Date(new Date - 30 * 1000)) ); // "30 sec. ago"
console.log( formatDateVerTwo(new Date(new Date - 5 * 60 * 1000)) ); // "5 min. ago"
// yesterday's date like 31.12.2016 20:00
console.log( formatDateVerTwo(new Date(new Date - 86400 * 1000)) );
// Solution 3
function formatDateVerThree(date) {
let dayOfMonth = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let hour = date.getHours();
let minutes = date.getMinutes();
let diffMs = new Date() - date;
let diffSec = Math.round(diffMs / 1000);
let diffMin = diffSec / 60;
let diffHour = diffMin / 60;
// formatting
year = year.toString().slice(-2);
month = month < 10 ? '0' + month : month;
dayOfMonth = dayOfMonth < 10 ? '0' + dayOfMonth : dayOfMonth;
hour = hour < 10 ? '0' + hour : hour;
minutes = minutes < 10 ? '0' + minutes : minutes;
if (diffSec < 1) {
return 'right now';
} else if (diffMin < 1) {
return `${diffSec} sec. ago`
} else if (diffHour < 1) {
return `${diffMin} min. ago`
} else {
return `${dayOfMonth}.${month}.${year} ${hour}:${minutes}`
}
}
728x90
function getSecondsToTomorrowVerOne() {
const now = new Date();
const tomorrow = new Date(now.getFullYear(), now.getMonth(), now.getDate() + 1);
const diff = tomorrow - now;
return Math.round(diff / 1000);
}
function getSecondsToTomorrowVerTwo() {
const now = new Date();
const hour = now.getHours();
const min = now.getMinutes();
const sec = now.getSeconds();
const totalSecondsToday = (hour * 3600) + (min * 60) + sec;
const totalSecondsInADay = 86400;
return totalSecondsInADay - totalSecondsToday;
}
console.log( getSecondsToTomorrowVerOne() );
console.log( getSecondsToTomorrowVerTwo() );
728x90
function getLastDayOfMonth(year, month) {
const months = [
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'
];
const date = new Date(year, month + 1, 0);
return `Last day of ${months[month]}, ${year} is ${date.getDate()}`;
}
console.log( getLastDayOfMonth(2012, 0) ); // 31
console.log( getLastDayOfMonth(2012, 1) ); // 29
console.log( getLastDayOfMonth(2013, 1) ); // 28

+ Recent posts