Skip to content

Instantly share code, notes, and snippets.

View pankajladhar's full-sized avatar
🏠
Working from home

Pankaj Ladhar pankajladhar

🏠
Working from home
View GitHub Profile
@pankajladhar
pankajladhar / encode-params.js
Created April 30, 2018 19:00
Encode an object into query string
const params = {id: '1', name: 'foo', isAcive: false};
const query = '?' + Object.keys(params)
.map(param =>
encodeURIComponent(param) + '=' + encodeURIComponent(params[param])
)
.join('&');
console.log (query) // ?id=1&name=foo&isAcive=false
@pankajladhar
pankajladhar / ES6-Cheatsheet.md
Last active June 20, 2018 22:23
ES6 Cheatsheet

Usage of Reduce method

const posts = [
    {id: 1, upVotes: 2},
    {id: 2, upVotes: 89},
    {id: 3, upVotes: 1}
];

const totalUpVotes = posts.reduce((acc, curr)=>{
    return acc + curr.upVotes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Layout Markup</title>
</head>
var console=(function(oldCons){
logMethod = function(color, msg){
oldCons.log('%c' + msg , 'color:' + color);
}
return {
violet: function(msg){
logMethod("#9400D3", msg)
},
indigo: function(msg){
@pankajladhar
pankajladhar / Overrideconsole.js
Created February 19, 2018 07:51
This code snippet will default console.log. It will print method name with message
var console=(function(oldCons){
return {
log: function(){
oldCons.log('%c' + "[ " + arguments.callee.caller.name + " method ]", 'background: #222; color: #bada55', arguments[0]);
}
};
}(window.console));
window.console = console;
@pankajladhar
pankajladhar / Stack.js
Last active February 12, 2018 05:50
Implementation of Stack in javascript using Factory Approach
/*
Stack is a linear data structure which follows a particular order in which the operations are performed.
The order may be LIFO(Last In First Out) or FILO(First In Last Out).
Mainly the following three basic operations are performed in the stack:
Push: Adds an item in the stack.
Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed.
If the stack is empty, then it is said to be an Underflow condition.
@pankajladhar
pankajladhar / ExpressionMatcher.js
Created January 13, 2018 08:08
ExpressionMatcher is take the str parameter being passed and return "Expression Matched" or "Expression Not Matched"
/*
ExpressionMatcher is take the str parameter being passed
and return "Expression Matched" or "Expression Not Matched"
@param {string} str
@param {array} openExpArr <optional>
default value ['[', '{', '(']
@param {array} closeExpArr <optional>
default value [')', '}', ']']
@returns
@pankajladhar
pankajladhar / ReverseString.js
Created January 13, 2018 07:36
Reverse a string
/*
ReverseString(str) take the str parameter being passed and return the string in reversed order.
For example: if the input string is "Hello World and Coders"
then your program should return the string sredoC dna dlroW olleH.
*/
const ReverseString = (str) => {
let reversedString = "";
for (let i = str.length - 1 ; i >= 0; i--) {
reversedString = reversedString + str[i]
@pankajladhar
pankajladhar / VowelCount.js
Created January 13, 2018 07:26
Count vowels available in string
/*
VowelCount is javascript function which takes string as input
return count of vowels available or 0 if no vowels available
*/
const VowelCount = (str) =>{
const vowelArray = ['a', 'e', 'i', 'o', 'u'];
let counter = 0;
for (let i = 0; i < vowelArray.length; i++) {
for (let j = 0; j < str.length; j++) {