Skip to content

Instantly share code, notes, and snippets.

View abhinavnigam2207's full-sized avatar
😎

Abhinav Nigam abhinavnigam2207

😎
View GitHub Profile
/* ******************** If Else/Switch Way ********************* */
const shape = 'circle';
const getArea1 = () => {
if (shape === 'circle') {
getCircleArea();
} else if (shape === 'triangle') {
getTriangleArea();
} else if (shape === 'rectangle') {
getRectangleArea();
/* ******************** If Else ********************* */
const integer =7;
let resp = '';
if (integer >= 10) {
resp = '2 digit integer';
} else {
resp = '1 digit integer';
}
@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,
@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 / 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;
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 {
const debounce = (func, delay) => {
let clearTimer;
return function() {
const context = this;
const args = arguments;
clearTimeout(clearTimer);
clearTimer = setTimeout(() => func.apply(context, args), delay);
}
}
@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);
}
@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 / 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;
}
}