Array Reduce Transformation (Transforms) - Leetcode 2626 - JavaScript 30-Day Challenge

preview_player
Показать описание

Solving day 6 of leetcode's 30-day javascript challenge. Today we continue with functional programming concepts using the Reduce operation.

0:00 - Read the problem
0:30 - Built-in Reduce usage
3:15 - Reduce solution
3:45 - For-loop solution
5:22 - For-each solution

leetcode 2626

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

the reduce function will automatically take the first array item as init if we don't pass i think you missed that part

nanduksathyan
Автор

I just wanna ask that when you allot the time to solve these for real? Do you do these in the morning before work or record videos after work at night? Cause editing and uploading also come in there, right?
Anyhow, love these constant upload. LY

jiraiya
Автор

have any of your co-workers / candidates you have interviewed at Google recognized you from your voice

julianelmasry
Автор

Here is a recursive solution haha i got bored.



type Fn = (accum: number, curr: number) => number

function reduce(nums: number[], fn: Fn, init: number): number {



let helper = (fn:Fn, total:number, current:number, idx:number) => {

if (idx == nums.length) {
return total;
}

return helper(fn, fn(total, current), nums[idx + 1], idx + 1);



}


return helper(fn, init, nums[0], 0)




};

Eddiek
Автор

Day 6 of doing the 30-day challenge with NeetCode!!

I spotted a slightly cleaner way of using foreach, which is: nums.forEach(n => res = fn(res, n)). no curly brackets and no parenthesis around n. Not a big deal, just wanted to share :D

vixguy
Автор

I listen just so things stay fresh. I get irritated when Im paying for a CS degree and I can pick up a rock and identify its mineral composition but I blank writing boilerplate.

lakewobegonesbest
welcome to shbcf.ru