Last active
July 9, 2019 04:35
-
-
Save kavitshah8/d8ba19d09334d8e08a1f209fdbacb597 to your computer and use it in GitHub Desktop.
Frequently Asked Javascript Interview Questions
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.bind1 = function (scope) { | |
let fn = this | |
let prefixArgs = Array.prototype.slice.call(arguments, 1) | |
return function() { | |
let suffixArgs = Array.prototype.slice.call(arguments) | |
let args = prefixArgs.concat(suffixArgs) | |
return fn.apply(scope, args) | |
} | |
} | |
// Ex 1: Create a function with a preset leading argument | |
function list() { | |
// arguments is an array-like object. convert arguments to an array. | |
return Array.prototype.slice.call(arguments); | |
} | |
console.log( list(1, 2, 3) ) // [1, 2, 3] | |
var leadingThirtysevenList = list.bind1(null, 37) | |
console.log( leadingThirtysevenList() ) // [37] | |
console.log( leadingThirtysevenList(1, 2, 3) ) // [37, 1, 2, 3] | |
// Ex 2: Changing the scope of `this` | |
var foo = { | |
x: 3 | |
} | |
function bar (){ | |
console.log(this.x) | |
} | |
var boundFunc = bar.bind1(foo) | |
boundFunc() // 3 | |
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
let sentence = '' | |
function say () { | |
if ( !arguments[0] ) { | |
let s = sentence | |
sentence = '' | |
return s | |
} else { | |
sentence += arguments[0] + ' ' | |
// Note: This is not a recursion | |
return say | |
} | |
} | |
console.log( say('Foo')('bar')() ) // => "Foo bar " | |
console.log( say('Hi')('my')('name')('is')('Foo')() ) // "Hi my name is Foo " | |
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 add10 (a) { | |
return a + 10 | |
} | |
function compound (f) { | |
return function (b) { | |
return f(f(b)) | |
} | |
} | |
console.log( add10(10) ) // 20 | |
console.log( compound(add10)(10) ) // 30 | |
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 customSetInterval (cb, interval) { | |
return setTimeout( () => { | |
if (typeof cb == 'function') { | |
cb() | |
// Recurse | |
customSetInterval(cb, interval) | |
} else { | |
console.error(new Error('Expecting a function as a callback')) | |
} | |
}, interval) | |
} | |
function resetCustomSetInterval (id) { | |
clearTimeout(id) | |
} | |
function hello (){ | |
console.log('hello') | |
} | |
let id = customSetInterval(hello, 1000) | |
let id2 = customSetInterval('hello', 1000) // [Error: Expecting a function as a callback] | |
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 hexToRGB (hex) { | |
// remove whitespace from left and rihgt ends | |
hex = hex.trim() | |
if (hex.startsWith('#')) { | |
hex = hex.replace('#', '') | |
} | |
// convert short hand notation (f0f) to the regular notation (ff00ff) | |
if (hex.length == 3) { | |
let temp = '' | |
for (let i = 0; i < hex.length; i++) | |
temp += hex[i] + hex[i] | |
hex = temp | |
} | |
// check for additional white space between characters (f 0 f) | |
if (hex.length != 6 ) { | |
throw new Error(`hexToRGB was called with incorrect hex code. | |
Valid hex code is either three to four characters long with following characters '0123456789ABCDEFabcdef' `) | |
} | |
// check for valid characters from 0-F | |
let validHexChars = '0123456789ABCDEFabcdef' | |
for (let i = 0; i < hex.length; i++) { | |
if ( !validHexChars.includes(hex[i]) ) { | |
throw new Error(`hexToRGB was called with incorrect hex code. | |
Valid hex code is either three to four characters long with following characters '0123456789ABCDEFabcdef' `) | |
} | |
} | |
let RGB = [] | |
let r = parseInt(hex.substring(0, 2), 16) | |
RGB.push(r) | |
let g = parseInt(hex.substring(2, 4), 16) | |
RGB.push(g) | |
let b = parseInt(hex.substring(4, 6), 16) | |
RGB.push(b) | |
return RGB | |
} | |
//console.log( hexToRGB('ff00ff') ) // [255, 0, 255] | |
//console.log( hexToRGB('#ff00ff') ) // [255, 0, 255] | |
//console.log( hexToRGB('#f0f') ) // [255, 0, 255] | |
//console.log( hexToRGB('ff00fff') ) // hextToRGB was called with incorrect hex code. ... | |
//console.log( hexToRGB('ff00ffg') ) // hextToRGB was called with incorrect hex code. ... |
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 BOARD_SIZE = 3 | |
// For 3 * 3 board, stateOfTheGame = [row1, row2, row3, col1, col2, col3, diag1, diag2] | |
let stateOfTheGame = new Array(BOARD_SIZE * 2 + 2) | |
//Initialize the board with zero | |
stateOfTheGame.fill(0) | |
function isTicTacToeWon (row, column, player) { | |
// We will use -1 for 'ZERO' and +1 for player 'EX' | |
let point = (player == 'ZERO') ? -1 : 1 | |
// At every step we will have to update the relevant row, column and diagonal | |
// update row | |
stateOfTheGame[row] += point | |
// update column | |
stateOfTheGame[BOARD_SIZE + column] += point | |
// update diagonal1 | |
if (row == column) { | |
stateOfTheGame[2*BOARD_SIZE] += point | |
// case (2, 2) in 3 * 3 Board | |
let shouldUpdateDia2 = (BOARD_SIZE + 1) / 2 == column ? true : false; | |
if (shouldUpdateDia2) { | |
stateOfTheGame[2*BOARD_SIZE + 1] += point | |
} | |
} | |
// update diagonal2 | |
if (row + column == BOARD_SIZE + 1) { | |
stateOfTheGame[2*BOARD_SIZE + 1] += point | |
} | |
// We finish updating the game board. Let's see if we have a winner or not | |
let i = stateOfTheGame.indexOf(3) | |
let j = stateOfTheGame.indexOf(-3) | |
// If we have 3, -3 in the array it means we have a winner | |
return (i >= 0 || j >= 0) ? true : false; | |
} | |
console.log( isTicTacToeWon(0, 0, 'ZERO') ) // false | |
console.log( isTicTacToeWon(0, 1, 'ZERO') ) // false | |
console.log( isTicTacToeWon(0, 2, 'ZERO') ) // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
resetCustomSetInterval
function not work in customSetInterval.js line 14 if it has run once. because eachsetTimeout
return a new handle id