This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ******************** If Else/Switch Way ********************* */ | |
const shape = 'circle'; | |
const getArea1 = () => { | |
if (shape === 'circle') { | |
getCircleArea(); | |
} else if (shape === 'triangle') { | |
getTriangleArea(); | |
} else if (shape === 'rectangle') { | |
getRectangleArea(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ******************** If Else ********************* */ | |
const integer =7; | |
let resp = ''; | |
if (integer >= 10) { | |
resp = '2 digit integer'; | |
} else { | |
resp = '1 digit integer'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debounce = (func, delay) => { | |
let clearTimer; | |
return function() { | |
const context = this; | |
const args = arguments; | |
clearTimeout(clearTimer); | |
clearTimer = setTimeout(() => func.apply(context, args), delay); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function.prototype.myCall = function(context, ...args) { | |
context.fnName = this; | |
context.fnName(...args); | |
} | |
/* Usage*/ | |
function showProfileMessage(message) { | |
console.log(message, this.name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(a) { | |
return function(b) { | |
if(b){ | |
return sum(a+b); | |
} | |
return a; | |
} | |
} | |
NewerOlder