Last active
March 22, 2025 09:31
-
-
Save abhi11210646/394923581a336ebd2a5fb36c8582eca0 to your computer and use it in GitHub Desktop.
Javascript Interview Questions
This file contains hidden or 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 curry(fn) { | |
| return function curried(...args){ | |
| if(args.length === fn.length){ | |
| return fn.apply(null, args); | |
| }else { | |
| return (...nextArgs)=>curried(...args, ...nextArgs); | |
| } | |
| } | |
| } | |
| // Test function with four parameters | |
| const multiply = (a, b, c, d) => a * b * c * d; | |
| const curriedMultiply = curry(multiply); | |
| console.assert(curriedMultiply(2)(2)(2)(2) === 16, 'curriedMultiply(2)(2)(2)(2) should return 16'); | |
| console.assert(curriedMultiply(2, 2)(2, 2) === 16, 'curriedMultiply(2, 2)(2, 2) should return 16'); | |
| console.assert(curriedMultiply(2, 2, 2, 2) === 16, 'curriedMultiply(2, 2, 2, 2) should return 16'); | |
| console.assert(curriedMultiply(2)(2, 2, 2) === 16, 'curriedMultiply(2)(2, 2, 2) should return 16'); | |
| console.log('All tests passed!'); |
This file contains hidden or 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
| // custom Promise.all implementation | |
| function PromiseAll(promises){ | |
| return new Promise((resolve, reject)=>{ | |
| const results = []; | |
| let count = 0; | |
| for (let i = 0; i< promises.length; i++) { | |
| let p = promises[i]; | |
| p.then(res=>{ | |
| results[i] = res; | |
| count++; | |
| if(count == promises.length) { | |
| resolve(results); | |
| } | |
| }).catch(reject) | |
| } | |
| }); | |
| } | |
| // application | |
| function task1(t) { | |
| return new Promise(resolve => { | |
| setTimeout(()=>resolve("task1 resolved"), t); | |
| }) | |
| } | |
| function task2() { | |
| return Promise.resolve("task2 resolved"); | |
| } | |
| function task3(){ | |
| const err = new Error("task3 failed.") | |
| return Promise.reject(err); | |
| } | |
| try { | |
| console.log("*****Native Promise.all*****"); | |
| const res1 = await Promise.all([task1(1500), task2()]); | |
| console.log("Response 1:", res1); | |
| const res2 = await Promise.all([task2(), task3()]); | |
| console.log("Response 2",res2); | |
| }catch (err) { | |
| console.log("error:", err.message); | |
| } | |
| try { | |
| console.log("*****Custom Promise.all*****"); | |
| const res1 = await PromiseAll([task1(1500), task2()]); | |
| console.log("Response 1:", res1); | |
| const res2 = await PromiseAll([task2(), task3()]); | |
| console.log("Response 2",res2); | |
| }catch (err) { | |
| console.log("error:", err.message); | |
| } | |
| // // Output: | |
| // *****Native Promise.all***** | |
| // Response 1: [ 'task1 resolved', 'task2 resolved' ] | |
| // error: task3 failed. | |
| // *****Custom Promise.all***** | |
| // Response 1: [ 'task1 resolved', 'task2 resolved' ] | |
| // error: task3 failed. |
This file contains hidden or 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
| // debounce function implementation | |
| function debounce(fn, t) { | |
| let timeout; | |
| return function (...args){ | |
| if(timeout) clearTimeout(timeout); | |
| timeout = setTimeout(()=>fn(...args), t); | |
| } | |
| } | |
| // application using debounce | |
| function search(i){ | |
| console.log("Hello:",i); | |
| } | |
| const debounceFn = debounce(search, 1000); | |
| debounceFn(1); | |
| await sleep(100); | |
| debounceFn(2); | |
| debounceFn(3); | |
| await sleep(1000); | |
| debounceFn(4); | |
| debounceFn(4); | |
| // Output: | |
| // Hello: 3 | |
| // Hello: 4 | |
| // utility function | |
| function sleep(t) { | |
| return new Promise(resolve => { | |
| setTimeout(resolve, t); | |
| }) | |
| } |
This file contains hidden or 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
| // throttle method #1 | |
| function throttle(fn, t) { | |
| let timeout=0; | |
| return function(...args) { | |
| if(new Date().getTime() - timeout >= t) { | |
| timeout = new Date().getTime(); | |
| return fn(...args); | |
| } | |
| } | |
| } | |
| // throttle method #2 | |
| function throttle2(fn, t) { | |
| let timeout=null; | |
| return function(...args) { | |
| if(!timeout) { | |
| fn(...args); | |
| timeout = setTimeout(()=>{ | |
| timeout = null; | |
| }, t); | |
| } | |
| } | |
| } | |
| // application use case | |
| function log(msg,i){ | |
| console.log(msg,i); | |
| } | |
| const throttlefn = throttle2(log, 5000); | |
| let i = 0; | |
| setInterval(()=>throttlefn("logging...", ++i), 100) |
This file contains hidden or 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 promiseWithTimeout(fn, t) { | |
| return new Promise((resolve, reject)=>{ | |
| let timeout = setTimeout(()=>{ | |
| reject(new Error("Promise timed out!")) | |
| },t) | |
| fn | |
| .then(resolve) | |
| .catch(reject) | |
| .finally(()=>{ | |
| clearTimeout(timeout); | |
| }); | |
| }); | |
| } | |
| const fetchTodos1 = fetch("https://jsonplaceholder.typicode.com/todos/1"); | |
| try { | |
| const res = await promiseWithTimeout(fetchTodos1, 1000); | |
| console.log("Response Status1:", res.status); | |
| }catch(err){ | |
| console.log("Error1:", err.message); | |
| } | |
| const fetchTodos2 = fetch("https://jsonplaceholder.typicode.com/todos/2"); | |
| try { | |
| const res = await promiseWithTimeout(fetchTodos2, 100); | |
| console.log("Response Status2:", res.status); | |
| }catch(err){ | |
| console.log("Error2:", err.message); | |
| } | |
| // Output: | |
| // Response Status1: 200 | |
| // Error2: Promise timed out! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment