Skip to content

Instantly share code, notes, and snippets.

View abhinavnigam2207's full-sized avatar
๐Ÿ˜Ž

Abhinav Nigam abhinavnigam2207

๐Ÿ˜Ž
View GitHub Profile
@abhinavnigam2207
abhinavnigam2207 / myNumber.js
Created March 5, 2019 10:39
Make the below code work (Asked in amazon interview)
/*
Make the below function calling work:
myNumber(10)
.subtract(3)
.add(10,1,3)
.value();
myNumber(10)
.add(7)
@abhinavnigam2207
abhinavnigam2207 / sum-curry-arity.js
Last active March 5, 2019 10:52
Make the syntax work sum(1)(2)(3).......(n) [Asked in multiple interviews]
function sum(a) {
var resp = a;
function innerSum(b) {
resp += b;
return innerSum;
}
innerSum.toString = function(){
return resp;
}
return innerSum;
@abhinavnigam2207
abhinavnigam2207 / curry-sum-arity.js
Created March 5, 2019 11:11
Make the syntax work sum(1)(2)(3).......() [Asked in multiple interviews]
function sum(a) {
return function(b) {
if(b){
return sum(a+b);
}
return a;
}
}
@abhinavnigam2207
abhinavnigam2207 / bind-polyfill.js
Last active April 20, 2023 05:22
Bind polyfill
Function.prototype.myBind = function(...boundArgs) {
let func = this;
let context = boundArgs.shift();
if (typeof this !== "function") {
throw new Error(this + "cannot be bound as it's not callable");
}
return function(...targetArgs) {
func.apply(context, args.concat(targetArgs));
@abhinavnigam2207
abhinavnigam2207 / call-polyfill.js
Last active November 5, 2021 03:31
Call Polyfill
Function.prototype.myCall = function(context, ...args) {
context.fnName = this;
context.fnName(...args);
}
/* Usage*/
function showProfileMessage(message) {
console.log(message, this.name);
}
const debounce = (func, delay) => {
let clearTimer;
return function() {
const context = this;
const args = arguments;
clearTimeout(clearTimer);
clearTimer = setTimeout(() => func.apply(context, args), delay);
}
}
const throttle = (func, limit) => {
let lastFunc
let lastRan
return function() {
const context = this
const args = arguments
if (!lastRan) {
func.apply(context, args)
lastRan = Date.now()
} else {
@abhinavnigam2207
abhinavnigam2207 / rewards.js
Created March 22, 2019 18:48
reward calculating simple problem
// A retailer offers a rewards program to its customers, awarding points based on each recorded purchase.
// A customer receives 2 points for every dollar spent over $100 in each transaction,
// plus 1 point for every dollar spent over $50 in each transaction
// (e.g. a $120 purchase = 2x$20 + 1x$50 = 90 points).
// Given a record of every transaction during a three month period,
// calculate the reward points earned for each customer per month and total.
function calculateRewards(price) {
if (price >=50 && price < 100) {
return price-50;
@abhinavnigam2207
abhinavnigam2207 / sum-multiple-notations.js
Created April 5, 2019 07:40
Sum multiple notations
function sum(...args) {
var resp = args.length==1 ? args[0] : args.reduce((key, acc) =>acc+=key,0);
function innerSum(...args1) {
let b = args1.length==1 ? args1[0] : args1.reduce((key, acc) =>acc+=key,0);
resp += b;
return innerSum;
}
innerSum.toString = function(){
return resp;
}
@abhinavnigam2207
abhinavnigam2207 / eightQueenProblem.js
Last active June 1, 2021 17:07
Eight Queens Problem Coder Byte
// https://www.coderbyte.com/editor/guest:Eight%20Queens:JavaScript
//
// Have the function EightQueens(strArr) read strArr which will be an array
// consisting of the locations of eight Queens on a standard 8x8 chess board
// with no other pieces on the board. The structure of strArr will be the
// following: ["(x,y)", "(x,y)", ...] where (x,y) represents the position of the
// current queen on the chessboard (x and y will both range from 1 to 8 where
// 1,1 is the bottom-left of the chessboard and 8,8 is the top-right). Your
// program should determine if all of the queens are placed in such a way where
// none of them are attacking each other. If this is true for the given input,