Skip to content

Instantly share code, notes, and snippets.

View completejavascript's full-sized avatar

Lam Pham completejavascript

View GitHub Profile
@completejavascript
completejavascript / smooth-scrolling.js
Created February 23, 2018 15:53
Smooth scrolling using jQuery
// Smooth scrolling
$("a[href*='#']:not([href='#'])").click(function () {
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
|| location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
@completejavascript
completejavascript / isInViewPort.js
Created February 23, 2018 16:15
Check whether element is in viewport
function isInViewPort(element){
let elementTop = $(element).offset().top;
let elementBottom = elementTop + $(element).outerHeight();
let viewPortTop = $(window).scrollTop();
let viewPortBottom = viewPortTop + $(window).height();
return elementBottom > viewPortTop && elementTop < viewPortBottom;
}
@completejavascript
completejavascript / setHashLocation.js
Created February 23, 2018 16:16
Set hash location for url address
// Set hash location for url address
function setHashLocation(id) {
var scrollmem = $('html,body').scrollTop();
window.location.hash = id;
$('html,body').scrollTop(scrollmem);
}
1) First fetch all changes:
$ git fetch --all
2) Then reset the master:
$ git reset --hard origin/master
3) Pull/update:
$ git pull
let person = {
name : 'Bob',
sayName : function() {
setTimeout(function() {
console.log(`I'm ${this.name}`);
}, 1000);
}
};
person.sayName();
@completejavascript
completejavascript / deduplicate-array.js
Last active May 15, 2018 03:30
Several methods to deduplicate an array.
let arr = [1, 5, 'a', 3, 'f', '3', 5, 3, 'b', 'e', 'f'];
let ans = deduplicate(arr);
console.log(ans);
// Expected output: [1, 5, "a", 3, "f", "3", "b", "e"]
function deduplicate(arr) {
let isExist = (arr, x) => {
for(let i = 0; i < arr.length; i++) {
if (arr[i] === x) return true;
let a = {
x: 1,
getX: function() {
return this.x;
}
};
let b = Object.create(a);
b.x = 10;
Object.prototype.objCustom = function() {};
Array.prototype.arrCustom = function() {};
let a = [3, 5, 7];
a.x = 'hello';
Object.defineProperty(a, 'y', {});
Object.defineProperty(a, 'z', {
enumerable: true
});
let arr = [1, "hi", {x : 2}, [3, 4]];
delete arr[1];
console.log(arr.length);
// What will console print?
let foo = function bar() {
return "hello";
}
console.log(typeof bar);
// What will console print?
// Ref: Named function expression
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/function