Skip to content

Instantly share code, notes, and snippets.

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

naveenrawat51@gmail.com naveenrawat51

🏠
Working from home
View GitHub Profile
@naveenrawat51
naveenrawat51 / JavascriptNativeCallApplyBind.js
Last active June 21, 2019 13:35
call(), apply() and bind() Implementation in Javascript, Implementation of Native JavaScript Methods call(), apply() and bind() --- naveenrawat51@gmail.com
+++++++++++++++++ call() Implementation +++++++++++++++++++++
Function.prototype.myCall = function(obj1){
let random = Math.random();
obj1[random] = this;
let args = [];
for( let i =1; i<arguments.length; i++ ){
args.push(arguments[i])
@naveenrawat51
naveenrawat51 / map_reduce_filter_forEach_call_apply_bind_polyfills.js
Created October 15, 2020 15:22
JavaScript map(), reduce(), filter(), forEach(), call(), apply() and bind() polyfills
Call(), apply() and Bind() Implementation:
// Bind implementation
Function.prototype.myBind = function(...args) {
let obj = this;
let params = args.slice(1);
return function(...args2) {
return obj.apply(args[0], [...params, ...args2]);
}
@naveenrawat51
naveenrawat51 / flatten_nested_object_and_nest_array_flatten.js
Last active January 31, 2021 14:03
flatten_nested_object in JavaScript
let flattenObj = function(inputObject) {
let flattenedObj = {};
for (let obj in inputObject) {
if (inputObject[obj].constructor === Object) {
let flatObj = flattenObj(inputObject[obj]);
for(let i in flatObj) {
flattenedObj[i] = flatObj[i];
}
class Node {
constructor(value) {
this.value = value;
this.right = null;
this.left = null;
}
}
class BinarySearchTree {
constructor() {
@naveenrawat51
naveenrawat51 / array_implementation_javascript.js
Last active August 11, 2022 09:36
Array implementation in javascript
class Myarray {
constructor(size) {
(this.data = {}), (this.length = 0);
for (let i = 0; i < size; i++) {
this.data[i] = undefined;
this.length++;
}
}
@naveenrawat51
naveenrawat51 / hash_table_javascript.js
Last active January 31, 2021 14:07
Hash Table implementation in javascript
class HashTable {
constructor(size) {
this.data = new Array(size);
}
// hash method
hashKey(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
hash = (hash + key.charCodeAt(i) * i) % this.data.length;
@naveenrawat51
naveenrawat51 / linked_list_javascript.js
Created January 31, 2021 14:06
linked list implementation in javascript
// new node structure
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
// linkedlist implementation
class LinkedList {
@naveenrawat51
naveenrawat51 / queue_javascript.js
Created January 31, 2021 14:08
Queue implementation in Javascript
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class Queue {
constructor() {
this.first = null;
@naveenrawat51
naveenrawat51 / Stack_Javascript.js
Created January 31, 2021 14:09
Stack implementation Javascript
class Mystack {
constructor() {
this.count = 0;
this.data = [];
}
push(item) {
this.data[this.count] = item;
this.count += 1;
return this.data;
@naveenrawat51
naveenrawat51 / getLastInMap.js
Created November 11, 2021 03:59 — forked from tizmagik/getLastInMap.js
ES6 Last Item in Map()
// Since ES6 Map()'s' retain their order of insertion, it can sometimes be useful to get the last item or value inserted:
export const getLastItemInMap = map => Array.from(map)[map.size-1]
export const getLastKeyInMap = map => Array.from(map)[map.size-1][0]
export const getLastValueInMap = map => Array.from(map)[map.size-1][1]
// Obviously getLastKey and getLastValue can reuse getLastItem, but for maximum portability I opted for verbosity.