Skip to content

Instantly share code, notes, and snippets.

@suryanshsingh2001
Created October 1, 2023 19:35
Show Gist options
  • Save suryanshsingh2001/100fd0203f8ede2b923ebf1e283408a2 to your computer and use it in GitHub Desktop.
Save suryanshsingh2001/100fd0203f8ede2b923ebf1e283408a2 to your computer and use it in GitHub Desktop.
30 Days of JS Leetcode in a nutshell
//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;
};
//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;
};
//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