Apply Transform over each Element in Array (Transform) - Leetcode 2635 - JavaScript 30-Day Challenge

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

Solving day 4 of leetcode's 30-day javascript challenge. Today we learn a bit about the functional programming aspects of javascript.

0:00 - Read the problem
0:27 - Map
1:16 - Functional Programming
2:03 - Loop solution
4:00 - Important bug
5:25 - Strategy Design Pattern

leetcode 2635

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

I just have to say, the way you clearly explain every thought along the thought process is super impressive. The way you explain all of the variarions in which you can accomplish each area of the code within the task is very satisfying appreciate the effort

brandonhyson
Автор

Why shouldn't we use a basic for loop instead of for...in which adds another step for casting "i" to "Number(i)" ?

alirmzn
Автор

[4:15] Actually, you can use this inside of functions. That's how it was done for almost twenty years. In fact, classes are mostly just syntactic sugar around functions.

function FactoryFunction(paramOne, paramTwo){
this.propOne = paramOne
this.propTwo = paramTwo
}

= function(){
++this.propOne
}

var instance = new FactoryFunction()

Ta-da! super() is just sugar around the call method.

function DerivedClass(paramOne, paramTwo, paramThree){
FactoryFunction.call(this, paramOne, paramTwo)
this.propThree = paramThree
}

var derivedInstance = new DerivedClass()

zombiefacesupreme
Автор

var map = function(arr, fn) {
const res =[];

for (let i in arr){
res.push(fn(arr[i], Number(i)));
}
console.log(res)
return res
};


let's dry run the original `for...in` code with `arr = [1, 2, 3]` and `fn = function plusone(n) { return n + 1; }`.

**Initial State:**

* `arr = [1, 2, 3]`
* `fn = function plusone(n) { return n + 1; }`
* `res = []` (empty array)

**Iteration 1:**

1. `for (let i in arr)`: The loop starts. `i` takes the first index (key) of `arr`, which is `"0"` (a string).
2. `res.push(fn(arr[i], Number(i)))`:
* `arr[i]` is `arr["0"]`, which is `1`.
* `Number(i)` converts `"0"` to `0` (a number).
* `fn(1, 0)` is called.
* `plusone(1)` returns `1 + 1 = 2`.
* `res.push(2)` adds `2` to `res`.
* `res` becomes `[2]`.

**Iteration 2:**

1. `for (let i in arr)`: `i` takes the next index, which is `"1"` (a string).
2. `res.push(fn(arr[i], Number(i)))`:
* `arr[i]` is `arr["1"]`, which is `2`.
* `Number(i)` converts `"1"` to `1` (a number).
* `fn(2, 1)` is called.
* `plusone(2)` returns `2 + 1 = 3`.
* `res.push(3)` adds `3` to `res`.
* `res` becomes `[2, 3]`.

**Iteration 3:**

1. `for (let i in arr)`: `i` takes the next index, which is `"2"` (a string).
2. `res.push(fn(arr[i], Number(i)))`:
* `arr[i]` is `arr["2"]`, which is `3`.
* `Number(i)` converts `"2"` to `2` (a number).
* `fn(3, 2)` is called.
* `plusone(3)` returns `3 + 1 = 4`.
* `res.push(4)` adds `4` to `res`.
* `res` becomes `[2, 3, 4]`.

**Loop Termination:**

* The loop finishes because all indices of `arr` have been processed.

**Return Value:**

* The function returns `res`, which is `[2, 3, 4]`.
* The console.log(res) outputs `[2, 3, 4]`

**Therefore, the output of `map([1, 2, 3], plusone)` using the `for...in` loop is also `[2, 3, 4]`.**

awesome-kartikey
Автор

Hi!
I am completely new to Competitive Programming.
Can you please tell what software do you use to write the code? And which language would you recommend for newbies like me?

AryanSingh-iuvt
Автор

This variant is faster and guaranteed
var map = function (arr, fn) {
if (arr.length === 0) {
return [];
}
const transformedArray = new Array(arr.length);
for (let i = 0; i < arr.length; i++) {
transformedArray[i] = fn(arr[i], i);
}
return transformedArray;
};

hrachhovhannisyan
Автор

6:40 why does he have to cast Number(i) because it returns a string but when indexing the array with "i" he doesnt cast it to a number ?

pastori
Автор

Why are we using "for in"? For loop is faster

hrachhovhannisyan
Автор

Hey neetcode why didn't you cast i into number in arr[i] great content btw

shivasai
Автор

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

vixguy
Автор

var map = function(arr, fn) {
const res =[];
let index = 0;

for (let i of arr){
res.push(fn(i, index));
index++
}
return res
};


let's do a dry run of the code with `arr = [1, 2, 3]` and `fn = function plusone(n) { return n + 1; }`.

**Initial State:**

* `arr = [1, 2, 3]`
* `fn = function plusone(n) { return n + 1; }`
* `res = []` (empty array)
* `index = 0`

**Iteration 1:**

1. `for (let i of arr)`: The loop starts, and `i` takes the first value from `arr`, which is `1`.
2. `res.push(fn(i, index))`:
* `fn(1, 0)` is called.
* `plusone(1)` returns `1 + 1 = 2`.
* `res.push(2)` adds `2` to the `res` array.
* `res` becomes `[2]`.
3. `index++`: `index` is incremented to `1`.

**Iteration 2:**

1. `for (let i of arr)`: `i` takes the next value from `arr`, which is `2`.
2. `res.push(fn(i, index))`:
* `fn(2, 1)` is called.
* `plusone(2)` returns `2 + 1 = 3`.
* `res.push(3)` adds `3` to the `res` array.
* `res` becomes `[2, 3]`.
3. `index++`: `index` is incremented to `2`.

**Iteration 3:**

1. `for (let i of arr)`: `i` takes the next value from `arr`, which is `3`.
2. `res.push(fn(i, index))`:
* `fn(3, 2)` is called.
* `plusone(3)` returns `3 + 1 = 4`.
* `res.push(4)` adds `4` to the `res` array.
* `res` becomes `[2, 3, 4]`.
3. `index++`: `index` is incremented to `3`.

**Loop Termination:**

* The loop finishes because all elements of `arr` have been processed.

**Return Value:**

* The function returns `res`, which is `[2, 3, 4]`.

**Therefore, the output of `map([1, 2, 3], plusone)` is `[2, 3, 4]`.**

awesome-kartikey
Автор

this is way too much thinking for such a simple task. Use a forEach instead.

wolandsmachine
join shbcf.ru