Created
October 1, 2023 19:35
-
-
Save suryanshsingh2001/100fd0203f8ede2b923ebf1e283408a2 to your computer and use it in GitHub Desktop.
30 Days of JS Leetcode in a nutshell
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
//2634. Filter Elements from Array | |
//filter function under the hood | |
/** | |
* @param {number[]} arr | |
* @param {Function} fn | |
* @return {number[]} | |
*/ | |
var filter = function(arr, fn) { | |
var a = []; | |
for(let i = 0; i < arr.length; i++) { | |
if(fn(arr[i], i)) { | |
a.push(arr[i]); | |
} | |
} | |
return a; | |
}; |
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
//2635. Apply Transform Over Each Element in Array | |
//map function under the the hood | |
/** | |
* @param {number[]} arr | |
* @param {Function} fn | |
* @return {number[]} | |
*/ | |
var map = function(arr, fn) { | |
for(let i = 0; i < arr.length; ++i) { | |
arr[i] = fn(arr[i], i); | |
} | |
return arr; | |
}; |
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
//2626. Array Reduce Transformation | |
//reduce function under the hood | |
/** | |
* @param {number[]} nums | |
* @param {Function} fn | |
* @param {number} init | |
* @return {number} | |
*/ | |
var reduce = function(nums, fn, init) { | |
for(let i = 0; i < nums.length; i++) { | |
init = fn(init, nums[i]); | |
} | |
return init; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment