Javascript Interview Questions ( map, filter and reduce ) - Polyfills and Output Based Questions

RoadsideCoder
20 Mar 202226:12

Summary

TLDRThis video script offers an in-depth exploration of JavaScript's array methods: map, filter, and reduce. It begins by explaining the purpose and functionality of each method, demonstrating their use through practical examples. The script then delves into creating polyfills for these methods from scratch, showcasing a deep understanding of JavaScript's prototypical inheritance. To solidify concepts, the script addresses common interview questions, providing solutions using map, filter, and reduce in various scenarios. The tutorial aims to prepare viewers for technical interviews, emphasizing the importance of these methods in JavaScript development.

Takeaways

  • 📚 The video discusses the use of JavaScript array methods: map, filter, and reduce, which are fundamental for transforming and computing data within arrays.
  • 🔍 Interviewers often ask about these methods in JavaScript interviews, including their functionality, differences, and how to implement them from scratch.
  • 🛠 The 'map' method is used to create a new array by applying a function to each element of an existing array, potentially returning a transformed array.
  • 🔎 The 'filter' method evaluates each element in the array against a condition; elements that meet the condition are included in the output array.
  • 🔄 The 'reduce' method condenses an array into a single value by applying a function to each element that accumulates a result.
  • 💡 The script provides examples of using these methods with JavaScript code, demonstrating how to multiply array elements, filter based on conditions, and calculate sums.
  • 👩‍🏫 The presenter also covers how to create polyfills for these methods, showing custom implementations of 'myMap', 'myFilter', and 'myReduce'.
  • 🔑 Differences between 'map' and 'forEach' are highlighted, emphasizing that 'map' returns a new array while 'forEach' does not return anything and can mutate the original array.
  • 🔗 The ability to chain methods after 'map' and 'filter' due to their return value is contrasted with 'forEach', which cannot be chained because it does not produce an array.
  • 📝 The script includes practical interview questions and demonstrates how to solve them using the discussed array methods, such as extracting student names in capital letters or filtering students based on marks.
  • 📈 Advanced use cases are also covered, such as combining 'map' and 'filter' to solve more complex problems, like adding grace marks to students and then calculating the sum of their scores.

Q & A

  • What are the primary purposes of the 'map', 'filter', and 'reduce' array methods in JavaScript?

    -The 'map', 'filter', and 'reduce' methods are used to iterate over an array and perform transformations or computations. The 'map' method creates a new array by applying a function to each element of the original array. 'Filter' applies a conditional statement to each element and returns a new array with elements that meet the condition. 'Reduce' reduces the array to a single value by applying a function that accumulates results.

  • Can you explain how the 'map' method works with an example?

    -The 'map' method applies a function to each element of an array and returns a new array with the results. For example, if you have an array of numbers and you want to create a new array with each number multiplied by 3, you can use 'map' to iterate over the original array and apply the multiplication operation.

  • How does the 'filter' method differ from the 'map' method?

    -While both 'map' and 'filter' iterate over array elements, 'map' transforms each element and returns a new array with all elements, 'filter' only returns elements that meet a specified condition. It does not transform the elements but rather selects a subset of the original array based on the condition.

  • What is the main difference between 'reduce' and the other two methods?

    -Unlike 'map' and 'filter', which return arrays, 'reduce' collapses the array into a single value. It applies a function to each element in the array, accumulating the result in an accumulator variable, which is then returned as the final output.

  • Can you provide an example of creating a polyfill for the 'map' method from scratch?

    -A polyfill for the 'map' method can be created by extending the Array prototype with a new function that iterates over the array elements, applies the callback function, and collects the results in a new array. The polyfill would look something like this: `Array.prototype.myMap = function(callback) { let temp = []; for (let i = 0; i < this.length; i++) { temp.push(callback(this[i], i, this)); } return temp; };`

  • What is the purpose of the callback function in the 'reduce' method?

    -The callback function in 'reduce' is responsible for accumulating the result. It takes four parameters: the accumulator, the current value, the current index, and the array being reduced. The accumulator accumulates the callback's return values; it's the accumulated value previously returned in the last invocation of the callback, or the initial value if provided.

  • How can you chain methods like 'map' and 'filter' together?

    -Chaining methods in JavaScript allows you to use the output of one method as the input for another. For example, you can first filter an array to get elements that meet a certain condition and then map the resulting array to transform those elements further.

  • What is the difference between using 'map' and a traditional 'for' loop for transforming array elements?

    -While both 'map' and a 'for' loop can be used to transform array elements, 'map' is more concise and functional, automatically creating a new array with the transformed elements. A 'for' loop, on the other hand, requires manual array creation and element assignment, which can be more error-prone and less readable.

  • Can you give an example of how to use 'filter' to get students with marks greater than 60?

    -You can use 'filter' to create a new array that only includes students who scored more than 60 marks. The syntax would be something like: `const highMarksStudents = students.filter(student => student.marks > 60);`

  • How can you combine 'map', 'filter', and 'reduce' to solve more complex array manipulation tasks?

    -For complex tasks, you can chain these methods to perform multiple operations in sequence. For example, you might first use 'filter' to select elements based on certain criteria, then 'map' to transform those elements, and finally 'reduce' to aggregate the results into a single value.

Outlines

00:00

📚 Introduction to JavaScript Array Methods

This paragraph introduces the importance of JavaScript's array methods 'map', 'filter', and 'reduce' in application development and interviews. The speaker plans to explain each method, create polyfills from scratch, and address common interview questions. The video begins with a demonstration of the 'map' method, showing how to create a new array by transforming each element of an existing array using a callback function. The explanation includes a practical example of multiplying each element of an array by a number and logging the result to the console.

05:01

🔍 Deep Dive into 'map' and 'filter' Methods

The speaker continues by contrasting the 'map' and 'filter' methods. 'Map' is used to create a new array by applying a transformation to each element, while 'filter' is used to create a new array with elements that meet a certain condition. The paragraph includes a step-by-step guide on creating a 'myMap' polyfill and demonstrates its functionality with an example. Following this, the 'filter' method is explained with an example of creating a new array that only includes elements greater than a specified value. A polyfill for 'filter' is also created and tested.

10:04

🔧 Exploring the 'reduce' Method and Its Polyfill

The 'reduce' method is introduced as a more complex function that condenses an array into a single value by applying a cumulative operation. The paragraph explains the parameters of the 'reduce' method, including the accumulator, current value, index, and array. A polyfill for 'reduce' is created, which involves initializing the accumulator with an initial value and iterating over the array elements to accumulate the result. The functionality of the 'reduce' polyfill is tested with an example that calculates the sum of an array's elements.

15:04

🛠 Creating Polyfills from Scratch

The speaker discusses creating polyfills for 'map', 'filter', and 'reduce' from scratch. The process involves adding custom functions to the Array prototype, allowing these methods to be used on any array. The 'myMap' and 'myFilter' functions are explained in detail, with examples of how they iterate over array elements and apply the callback function. The paragraph also includes a demonstration of how to test these custom functions to ensure they work as expected.

20:06

🤓 JavaScript Interview Questions and Techniques

This paragraph focuses on common JavaScript interview questions related to array methods. The speaker discusses the difference between 'map' and 'forEach', highlighting that 'map' returns a new array while 'forEach' does not return anything and can be used to modify the original array. The paragraph also covers how to chain methods like 'map' and 'filter' for more complex operations. Practical examples are given, such as transforming an array of student objects to meet specific criteria using 'map', 'filter', and 'reduce'.

25:08

📈 Advanced Use Cases and Interview Challenges

The final paragraph presents more complex interview questions that involve combining 'map', 'filter', and 'reduce' methods. The speaker demonstrates how to solve problems such as filtering students based on marks, adding grace marks, and calculating the total marks of eligible students. The paragraph emphasizes the importance of understanding these methods for JavaScript interviews and encourages viewers to practice and master them.

Mindmap

Keywords

💡Array Methods

Array methods are functions built into JavaScript that operate on arrays. They are essential for performing operations like iterating over elements, transforming data, and aggregating results. In the video, array methods are the central theme, with a focus on 'map', 'filter', and 'reduce', which are used to demonstrate array manipulation and computation.

💡Map

The 'map' method is an array method used to create a new array by transforming each element of an existing array according to a provided function. In the video, 'map' is used to multiply each element of an array by a number, demonstrating how to create a new array with modified elements without altering the original array.

💡Filter

'Filter' is another array method that creates a new array containing only those elements that pass a test implemented by the provided function. The video illustrates 'filter' by showing how to extract elements from an array that meet a certain condition, such as having a value greater than a specified number.

💡Reduce

The 'reduce' method is used to reduce the array to a single value. It executes a reducer function for each element in the array, accumulating a result in a variable called the accumulator. In the video, 'reduce' is explained by calculating the sum of all elements in an array, showcasing its use for aggregation.

💡Callback Function

A callback function is a function passed into another function as an argument, which is then invoked inside the outer function. In the context of array methods, callback functions define the transformation or condition applied to each element during 'map', 'filter', or 'reduce' operations. The video script includes examples of callback functions used with these methods.

💡Polyfill

A polyfill is a piece of code (or plugin) that provides the technology that you, as a developer, expect the browser to provide natively. In the video, the script author discusses creating polyfills for 'map', 'filter', and 'reduce' from scratch, demonstrating a deeper understanding of how these array methods work and how they can be implemented manually.

💡Iteration

Iteration refers to the process of repeating a process or operation multiple times, often used in programming to loop through elements in a collection. In the video, iteration is central to how 'map', 'filter', and 'reduce' work, as they each involve looping through array elements to apply a function or condition.

💡Interview Questions

The script mentions that 'map', 'filter', and 'reduce' are common topics in JavaScript interviews. Interview questions about these concepts test a candidate's understanding of array manipulation and functional programming techniques. The video aims to prepare viewers for such questions by explaining the methods and providing examples.

💡Chaining

Chaining in JavaScript refers to the ability to call multiple methods on an object in a single line of code. The video script explains that methods like 'map', 'filter', and 'reduce' can be chained to perform multiple operations in sequence, which is demonstrated by combining 'filter' and 'map' to process array data.

💡Accumulator

In the context of the 'reduce' method, an accumulator is a variable that holds the accumulated result of the reduction operation. It is initialized with the initial value provided to 'reduce' or with the first element of the array if no initial value is given. The video script uses the accumulator to sum up the elements of an array.

💡Conditional Statement

A conditional statement is a programming construct that allows different blocks of code to be executed depending on whether a condition is true or false. In the video, conditional statements are used within the 'filter' method to determine which elements should be included in the resulting array.

Highlights

Map, filter, and reduce are essential array methods in JavaScript, often discussed in interviews.

Map creates a new array by applying a function to each element of an existing array.

Filter includes elements in the output array that meet a certain condition.

Reduce collapses an array into a single value through a cumulative computation.

Creating polyfills for map, filter, and reduce helps understand how they work under the hood.

The map function is used to transform elements of an array into a new form without modifying the original array.

Filter allows for conditional logic to determine which elements are included in the resulting array.

Reduce is suitable for calculating a single value, such as a sum, from an array.

The accumulator in reduce holds the accumulated value after each iteration.

Differences between map and for-each are highlighted, such as map's ability to return a new array.

Map supports method chaining, unlike for-each, which does not return an array.

Practical examples demonstrate using map, filter, and reduce to manipulate and analyze student data.

Interview questions often require combining map and filter to solve complex data manipulation problems.

Reduce can be used to calculate the sum of marks for students meeting certain criteria.

Chaining filter after map allows for further refining the data based on additional conditions.

A complex interview question involves adding marks to students and then filtering and summing their scores.

The video provides insights into JavaScript interview questions and encourages viewers to subscribe for more content.

Transcripts

play00:00

map filter and reduce we all use this

play00:03

array methods a lot of times while

play00:04

developing our javascript applications

play00:06

and these are amongst one of the most

play00:08

asked questions in javascript interviews

play00:11

interviewers absolutely love to confuse

play00:13

candidates by questions like how they

play00:15

work difference between them creating

play00:17

these functions from scratch etc so map

play00:19

filter reduce are basically array

play00:21

methods that are used to iterate over an

play00:23

array and perform a transformation or

play00:25

computation

play00:26

each may or may not return a new array

play00:28

based on the result of the function

play00:31

so in this video i'll be explaining each

play00:33

one of them one by one make polyfills

play00:35

for them from absolute scratch and

play00:37

discuss some commonly asked interview

play00:39

questions so i've opened vs code over

play00:41

here with html css and javascript files

play00:45

and when i go on to run this

play00:47

we have this window over here

play00:49

so i'm gonna quickly open the console

play00:51

because that is where we are gonna see

play00:53

our output all right so let's start by

play00:55

discussing each map filter and reduce

play00:58

separately and this is a very basic

play01:00

question that interviewers ask on this

play01:02

topic so first we're gonna discuss what

play01:05

is map

play01:06

so the map method is used for creating a

play01:08

new array from existing one by applying

play01:11

a function to each one of the elements

play01:13

of the first array so let me show you if

play01:15

i go on and type const

play01:17

numps array and i'm gonna give let's say

play01:21

one two three

play01:23

four now what i wanna do is i want to

play01:25

multiply each of these elements by 5 and

play01:28

return a new array to me so i can create

play01:30

a new variable const

play01:32

multiply

play01:34

3

play01:36

and inside of this we're going to type

play01:38

nums and here map comes in the picture

play01:41

so i i'm going to type map

play01:43

and this takes a callback function so

play01:46

we're going to provide a function over

play01:47

here and this function takes three

play01:49

things first is the current element so

play01:52

it's going to iterate through all of

play01:53

these elements right so it's going to

play01:55

take the current element so let's say

play01:56

i'm going to give this num

play02:00

and second will be the index that is

play02:02

this 0

play02:04

1

play02:04

2 so

play02:07

index and the third will be the actual

play02:09

array

play02:10

that is this array so we're gonna say

play02:12

arr

play02:13

in this case we're not gonna use index

play02:15

or the array

play02:16

so what i'm gonna do over here is i'm

play02:18

gonna go inside and say return num

play02:21

into

play02:23

3 and what this will do is it's going to

play02:25

take each element of this array and

play02:27

multiply by 3 and return it and it's

play02:29

going to return a completely new array

play02:31

inside of this multiply 3. so let's go

play02:34

on and console.log this

play02:37

and let's see the output

play02:39

yep you see we have a completely new

play02:41

array with all of the elements

play02:43

multiplied by 3.

play02:45

now if you would like to see what this

play02:47

index does is

play02:48

let's say we add index in this as well

play02:52

so it should give us result like three

play02:55

like this

play02:56

one

play03:00

two into three plus one something like

play03:03

that so

play03:04

let's find out

play03:07

yep we have 3 7 11 and 15.

play03:10

so this is how

play03:12

a map array method works so let's see

play03:14

what filter is so the filter method

play03:17

takes each element in an array and it

play03:18

applies a conditional statement against

play03:21

it if the conditional returns true the

play03:23

element gets pushed into the output

play03:25

array if the condition returns false the

play03:27

element does not get pushed into the

play03:29

output array in short filter returns

play03:31

only those elements from the array which

play03:33

fulfills the provided criteria so let me

play03:35

show you so let's say we have const and

play03:38

what we want to do is we want to only

play03:40

return the elements which are more than

play03:42

two so i'm gonna give it more than

play03:45

to name

play03:47

and i'll say nums dot filter

play03:51

inside of this again this is going to

play03:52

take a callback function

play03:55

and this will again take those three

play03:57

things

play03:58

num

play03:59

index and the array so num and but in

play04:02

this case as well we don't need index

play04:04

and array so i'm just not going to write

play04:06

them

play04:06

so return if num is more than 2.

play04:11

let's console log this more than

play04:15

two

play04:16

let's find out

play04:17

yup you see

play04:19

we only returned the elements that were

play04:21

more than two inside of this more than

play04:24

two array so this is what filter does

play04:26

and you can clearly notice that its

play04:28

syntax is similar to map except the

play04:30

function returns true to keep the

play04:32

element or false to not return it now

play04:35

the reduce method is probably most

play04:37

complicated of all three the reduce

play04:39

method reduces an array of values down

play04:42

to just one value just like map and

play04:44

filter reduce also executes the callback

play04:46

for each element of the array so it

play04:49

receives two things so let me show you

play04:51

so if i write const

play04:53

sum so what we want over here is we want

play04:55

to make

play04:56

we want to calculate the sum of all of

play04:58

these elements we want to reduce this

play05:01

array

play05:02

down to just one value which is going to

play05:04

be the sum of this array so i'm going to

play05:06

write const sum equals

play05:08

nums dot reduce

play05:11

and now this is gonna take two things as

play05:13

you can see over here it's gonna take

play05:14

one callback function and an initial

play05:16

value

play05:17

so i'm gonna

play05:19

write the callback function over here

play05:21

and the second parameter is gonna be the

play05:22

initial value so i'm going to keep the

play05:23

initial value as 0.

play05:26

now this callback functions parameters

play05:28

are a little bit different from what map

play05:30

and filter had so this has an

play05:33

accumulator

play05:35

the current value

play05:37

the index and our array

play05:40

so what are these accumulator and the

play05:41

current value so accumulator is

play05:44

basically the result of the previous

play05:46

computation right now there is no

play05:48

computation at the beginning right so

play05:50

this is going to be zero initially the

play05:52

current value is the current element of

play05:54

the array obviously and then we have the

play05:56

index and the array so what we're going

play05:58

to do over here is

play06:00

we're going to return

play06:02

accumulator plus the current value

play06:05

so it's going to start by 0 and it's

play06:07

going to add 1 2 3 4. just like that

play06:10

it's going to make the sum of this

play06:12

complete array and return to us

play06:15

so let me just quickly console log this

play06:18

so let's see and here you see we get

play06:21

this 10 as the sum of this array awesome

play06:24

now don't worry if you're having trouble

play06:25

understanding them now we are going to

play06:27

discuss output questions on each one of

play06:29

them as we move forward in this video

play06:31

but let's first start with how we can

play06:33

create

play06:34

each one of them from absolute scratch

play06:36

that is their polyfills

play06:39

so first let's create the polyfill for

play06:41

map

play06:42

so i'm gonna write array

play06:45

dot prototype

play06:47

dot and i'm gonna give this some custom

play06:49

name so i'm gonna say let's say my map

play06:52

and don't worry if you don't know

play06:53

anything about prototypes i'm gonna

play06:55

bring a separate video on prototype and

play06:56

prototypal inheritance etcetera so do

play06:58

click on subscribe button if you're

play07:00

excited for that video so what this

play07:01

prototype is doing is it's adding this

play07:03

mymap function to the methods of this

play07:07

array in our current javascript file

play07:09

so

play07:11

this will take a function

play07:16

i'm not going to give this any name

play07:18

and for the parameters it's going to

play07:19

take a callback so let me write the map

play07:22

syntax over here quickly so we have

play07:25

array.map

play07:27

and

play07:28

map takes a callback

play07:31

which inside of it takes

play07:33

the current value index and the array so

play07:35

we're going to reference this and create

play07:37

the polyfill for our my map function

play07:40

okay now instead of this this takes a

play07:42

callback

play07:43

now

play07:45

so map returns a completely new error

play07:47

right so to create a new array we will

play07:49

need an empty array first so let's

play07:52

declare

play07:53

let

play07:54

temp

play07:55

to be an empty array and now inside this

play07:57

since we are going to iterate through

play07:59

each element of the array we're going to

play08:01

have a for loop

play08:04

which will start from 0 go to this dot

play08:07

length

play08:08

so i've written this this dot length

play08:09

over here is because obviously we're

play08:11

gonna when we use this my map function

play08:13

we're gonna write something like this

play08:15

array dot my map so since this is the

play08:19

part of this

play08:20

array so when we use this inside of it

play08:23

it's gonna reference our parent array so

play08:25

this here means basically the array that

play08:27

which we are working on now inside of

play08:29

this since we have this callback

play08:31

condition right so what we're gonna do

play08:34

we're gonna say temp

play08:35

that is this temporary output array

play08:39

temp dot push

play08:40

and what are we gonna push we're gonna

play08:42

push the computation of this callback

play08:45

i'll say call back and i'm gonna provide

play08:47

these three things the current element

play08:49

index and the array

play08:51

so the current element is this of this

play08:54

index so i'm going to write

play08:56

this that is our array and the index i

play09:01

second one is the index so i'm just

play09:02

going to write i and the third one is

play09:04

the actual array so i'm just going to

play09:05

write this

play09:06

and now i will return

play09:08

temp

play09:09

and that's it this is our custom map

play09:12

function congrats so let's try this out

play09:14

and see if it works as expected so this

play09:17

was the example that we discussed while

play09:19

understanding the map so let's remove

play09:21

this dot map over here and i'm going to

play09:22

use my map polyfill and let's save this

play09:26

and see

play09:28

yep it works as expected

play09:30

great now let's go on and create the

play09:32

polyfill for filter so to create the

play09:34

polyfill for filter it's going to be

play09:36

absolutely same as it was for map so i'm

play09:39

going to say array dot prototype dot

play09:41

filter

play09:43

and it's going to take a call back just

play09:45

like map

play09:46

and inside of this since this also

play09:48

returns an array so i'm gonna have let

play09:50

temp

play09:52

empty cool

play09:53

now again since we are iterating through

play09:55

each of the values so i'm gonna write

play09:57

for loop

play10:04

okay so things are absolutely same up

play10:06

until this point

play10:07

so what is the difference between map

play10:09

and filter

play10:10

so map basically returns each and every

play10:12

value and modifies it according to the

play10:15

condition of the callback right but

play10:17

filter only returns those values which

play10:19

satisfy the condition of callback

play10:22

not each and every element

play10:23

so in the map we used to do this temp

play10:25

dot push

play10:27

and then insert this we gave callback

play10:29

and the current element index and the

play10:31

array to it right but we're not going to

play10:33

do that over here

play10:36

we're going to first check

play10:38

if

play10:39

callback satisfies the condition so

play10:41

callback will have the current element

play10:43

that is this

play10:45

of i

play10:46

the index

play10:48

and the current array that is this

play10:52

and if this satisfies the condition then

play10:54

we're going to push

play10:55

the current element that is this of

play10:58

i just like that and the and in the end

play11:01

we're gonna return

play11:03

temp

play11:04

that's all that's all we need to do

play11:06

and i'm gonna name this my filter

play11:08

actually so that it doesn't clashes with

play11:10

the pre-existing filter function inside

play11:13

of the array prototype

play11:14

so here's that example that you

play11:16

discussed while understanding filter so

play11:19

let's replace

play11:20

this filter with my filter and test to

play11:24

see if it works

play11:26

which it does cool

play11:28

let's discuss the polyfill for reduce

play11:31

okay so the polyfill for reduce it's a

play11:33

little different from map and function

play11:35

as you know so it takes two things one

play11:37

is a callback

play11:40

and the other is the initial value so

play11:43

initial

play11:45

value just like that inside of this

play11:47

function we're gonna say callback

play11:50

comma initial value and this callback

play11:53

has how many things

play11:55

it has the accumulator

play11:57

the current value

play11:59

the index and the array

play12:01

so we're gonna work according to this

play12:03

syntax now instead of this

play12:05

we would first need to initialize the

play12:07

accumulator so with the initial value so

play12:11

i'll write where

play12:14

accumulator

play12:16

equals

play12:18

initial value okay so that's done

play12:21

now next obviously we're going gonna

play12:23

iterate through all of the elements of

play12:24

the array so we're gonna need the for

play12:26

loop now inside of this for loop

play12:28

so i missed one point while explaining

play12:31

reduce so what happens if the user fails

play12:34

to give the initial value so in our case

play12:37

we gave the initial value as zero right

play12:39

but what if he fails to give the initial

play12:41

value any initial value so what happens

play12:44

is accumulator takes the first element

play12:47

of the array as the initial value and

play12:49

the current value is assigned the second

play12:52

element of the array so we're going to

play12:54

check over here

play12:56

accumulator equals

play12:59

accumulator

play13:00

we're going to check

play13:02

if there's anything inside of the

play13:03

accumulator then that's fine we're going

play13:05

to go inside of the callback and execute

play13:07

it

play13:08

by providing the accumulator the current

play13:10

value which is this of i

play13:13

and the index and the current array

play13:16

that is this

play13:18

cool

play13:19

but what if it doesn't have anything

play13:21

inside of it then we will first go on

play13:23

and assign it the first element of the

play13:25

array so we will say this of i

play13:29

oops i

play13:30

just like that

play13:32

and now when it comes to another loop

play13:34

it's gonna already have the initial

play13:36

value as the first element of the array

play13:38

now after this loop ends

play13:40

it's gonna have the final value of our

play13:42

reduce function inside this accumulator

play13:44

so we're gonna just return

play13:47

accumulator just like that and that's it

play13:49

this is our polyfill for reduce function

play13:53

let's test it out so again we have the

play13:55

example that we used to understand the

play13:57

reduce so i'm just gonna

play14:01

replace it with my reduce

play14:03

save it let's see

play14:06

yep still got the same output

play14:09

so i hope you are able to understand how

play14:10

you can go on and create these functions

play14:12

from absolute scratch and these are

play14:14

really important for the javascript

play14:16

interviews now there's one question

play14:18

which is really commonly asked in these

play14:20

javascript interviews that is map versus

play14:23

4h so this question was asked to me in

play14:25

my an academy interview on which i have

play14:27

made a complete video and here is the

play14:29

clip to that question what is the

play14:31

difference between map and for each

play14:34

so to explain this we're gonna take an

play14:36

array

play14:39

with let's say two five three four

play14:42

some random values

play14:44

now first of all what are map and for

play14:46

each these both are array functions to

play14:49

loop through the items of the array so

play14:52

if i go on and say arr

play14:54

dot map

play14:56

and we're gonna provide a call back over

play14:58

here so just like this

play15:02

and now we can perform any operation on

play15:04

this array so let's say return

play15:11

ar

play15:12

plus 2. so what this will do is it's

play15:14

going to return the whole array with two

play15:17

added to each item of the array it's not

play15:20

going to modify the original array

play15:22

whereas if we do this

play15:25

for each this performs like a normal for

play15:27

loop but this doesn't return anything

play15:29

just like map does so if i go on and say

play15:34

const map result

play15:38

and

play15:39

const

play15:40

for each

play15:42

result

play15:45

and go on and console log both of these

play15:49

now let's go to our browser and open the

play15:52

console so we get an array in the result

play15:55

of the map so it returned us the array

play15:57

but didn't modify the original array so

play15:59

it returned us a new array with all of

play16:01

the items

play16:02

having a plus two

play16:04

but the for each didn't return us

play16:05

anything so both of these functions have

play16:08

a different use case let's say if we

play16:10

want to modify this array so what i'm

play16:12

going to do i'm going to take the index

play16:17

so what this will do is it will modify

play16:19

the original array so let's say i'm

play16:22

going to add 3 to it and now i'll

play16:25

console log this array

play16:27

this was the array from the map and this

play16:29

was the modified array which was arr our

play16:32

original array now this was the first

play16:34

difference now the second difference is

play16:35

that you can chain stuff on map so i can

play16:39

say dot and something we can let's say

play16:42

if we want to do dot filter and do some

play16:45

operation inside of this filter so we

play16:46

can do that with map but in forage since

play16:50

it doesn't return any array we cannot

play16:52

chain other methods after it

play16:55

all right now let's discuss some output

play16:57

based questions which involve the use of

play16:59

map filter and reduce so let's save the

play17:02

interview provided with this array

play17:04

called students which has the data for

play17:06

four students with name roll number and

play17:08

marks inside of each of the object so

play17:10

the first question that interviewer gave

play17:12

is that you need to return only the name

play17:14

of students in capital letters so there

play17:17

can be multiple approach to this

play17:18

question you can either use the

play17:20

traditional for loop or you can use the

play17:22

for each or you can use a map so let's

play17:25

see what would have been the case if we

play17:27

used the traditional for loop we would

play17:29

have created a new array first of all so

play17:32

let names

play17:34

names

play17:37

we would have ran a for loop

play17:39

inside this for loop we would have said

play17:41

names

play17:43

dot

play17:44

push we're gonna push only the name of

play17:47

the student and we will take each of the

play17:49

element so i'll say i

play17:52

and take the name and make it to

play17:55

upper case and that's it and then we

play17:58

will print this

play18:00

console.log this names

play18:03

let's see the output

play18:04

yep

play18:05

we get the output that's fine but this

play18:07

code looks a little messy let's clean

play18:10

this up by using map

play18:13

so i'll say const

play18:16

names and i'm going to make this one

play18:18

liner so i'll say students

play18:21

dot map and let's says 2 for the current

play18:25

element

play18:26

and i'll says 2 dot

play18:30

name

play18:32

name

play18:33

dot 2 upper case

play18:36

that's it that's all we needed to do

play18:38

and we are done with this just one line

play18:40

of code let's see the output and we get

play18:43

the same output all right pause if

play18:45

you're not yet following me on twitter

play18:47

go to twitter.com push underscore eon or

play18:49

click the link in the description down

play18:51

below and hit that follow button right

play18:53

now

play18:54

i'm waiting for you

play18:56

i'm still waiting

play18:59

okay fine let's continue with the video

play19:01

now the second output question is return

play19:03

only the details of those students who

play19:05

scored more than 60 marks so i want you

play19:07

to try this yourself and what do you

play19:09

think which function are we gonna use to

play19:12

solve this question pause the video

play19:13

right now and solve it okay i hope you

play19:15

have solved it yourself so i'm gonna

play19:17

show you the answer so we will have a

play19:20

variable called

play19:22

details

play19:23

and inside of it i'm going to take

play19:24

students

play19:26

dot

play19:27

filter we're going to use filter

play19:28

function now we will take s2 and we will

play19:32

add the condition what was the condition

play19:34

it has to the marks have to be more than

play19:36

60. so 2

play19:38

dot marks

play19:40

to be more than 60 and that's all that's

play19:43

all we needed to do to calculate the

play19:45

answer and let's see the output

play19:48

we get two

play19:50

elements of the array and both of these

play19:52

students had marks more than 60 and

play19:54

those who didn't have marks more than 60

play19:57

were filtered out from the array now the

play20:00

next question can be returned the

play20:02

details of only those students who have

play20:04

marks greater than 60 and roll number

play20:06

greater than 15 so i think it's gonna be

play20:09

same as the previous question so i'll

play20:11

write

play20:12

this

play20:16

value and instead of it i'll write

play20:19

students dot

play20:21

marks

play20:22

have to be more than 60. okay that's

play20:24

fine and the other condition is

play20:26

that roll number has to be greater than

play20:28

15 so

play20:30

2 dot roll number has to be greater than

play20:33

15.

play20:34

let's see the output for this i'm going

play20:36

to console.log details

play20:40

and only one of the students had

play20:43

marks more than 60 and roll number

play20:46

greater than 15. now the next question

play20:48

is we need to calculate the sum of marks

play20:51

of all of the students

play20:53

so you already know what we're going to

play20:54

use here

play20:55

we're going to use reduce

play20:58

so reduce we will take the accumulator

play21:02

and the current value

play21:06

oops i mean

play21:07

comma

play21:09

now here we will just simply say

play21:11

accumulator plus

play21:14

the current value

play21:16

dot marks that is the current element

play21:19

that is this one and we're going to take

play21:21

the marks so we took the marks over here

play21:24

and here you can provide the initial

play21:25

value as 0.

play21:28

and let's see const sum equals and i'm

play21:31

gonna console.log

play21:33

sum

play21:34

so let's see

play21:36

yep we get the sum of all of the marks

play21:38

of all of the students

play21:40

great now these questions were really

play21:42

straightforward now there can be

play21:44

questions which may involve use of more

play21:46

than one function that is you may need

play21:48

to use map or filter together filter or

play21:51

reduce together so let's see those type

play21:53

of questions our next question is to

play21:55

return only names of the students who

play21:57

scored more than 60.

play21:59

so we already seen this part

play22:02

and we already saw this part as well in

play22:04

we saw this part in map and we saw this

play22:06

part in filter so we're gonna need to

play22:08

combine both of them

play22:10

somehow

play22:12

filter first of all we're going to

play22:13

filter out those students which have

play22:16

less marks than 60 or more marks than 60

play22:19

sorry i'll say s2

play22:22

marks oops i mean 2 dot marks has to be

play22:26

more than 60. that's fine this is gonna

play22:28

return us all of the students with marks

play22:30

more than 60. now

play22:32

as i discussed in my map versus for each

play22:35

video and i forgot to mention this over

play22:38

here in this video that all of these

play22:40

functions map filter and reduce

play22:43

allows chaining we can chain other

play22:46

functions in front of these functions

play22:49

so

play22:50

the output of this

play22:52

filter

play22:53

can be chained by typing dot map or dot

play22:56

filter or dot reduce so i'm gonna map

play22:59

through the result of this

play23:02

condition right here so i will say

play23:05

s2

play23:07

and 2 dot name we are only supposed to

play23:10

return the name

play23:11

so this should do it let's see names

play23:16

and awesome so we only got the name of

play23:19

those students which have scored more

play23:21

than 60. okay so our last question is a

play23:24

bit complicated

play23:26

so this says return total marks for the

play23:28

students with marks greater than 60

play23:31

after 20 marks have been added to those

play23:33

students who scored less than 60. so all

play23:36

of those students who have marks less

play23:38

than 60 got a grace of 20 more marks so

play23:41

20 marks will be added to those students

play23:43

and after that we are supposed to return

play23:45

only those students which have

play23:48

marks greater than 60 after the addition

play23:50

of 20 marks of course and then for those

play23:52

particular students we are supposed to

play23:54

calculate total marks the sum of total

play23:56

marks of those students so let's see

play23:58

what is the constant total marks

play24:01

and i will say

play24:02

students dot first of all we're gonna do

play24:05

the map

play24:07

so inside this map i will take

play24:11

student

play24:13

and i will say

play24:15

if

play24:16

student

play24:18

marks are less than 60.

play24:21

we're going to provide them

play24:23

a 20 marks grace

play24:24

so i will says 2 dot marks

play24:29

and we're going to add

play24:31

20 to it and then we will return

play24:36

that particular student that's all let's

play24:38

see the output for this one

play24:40

before moving forward

play24:46

so

play24:47

these students had less marks than 60

play24:50

caution and they'll breathe

play24:51

so

play24:53

they got 20 marks extra

play24:55

cool so this one works now we are going

play24:57

to chain

play24:59

another function that is filter over

play25:01

here

play25:02

and we're going to filter out all those

play25:03

students

play25:04

with marks greater than 60. so i'll say

play25:07

student and student dot marks

play25:11

greater than 60.

play25:13

let's see the output now

play25:17

okay one of the students which had

play25:19

less marks than 60 got removed and now

play25:22

we're supposed to return the total marks

play25:24

for the students so we will chain

play25:27

another reduce over here to calculate

play25:30

the sum

play25:31

so

play25:32

accumulator and the current value

play25:36

just like previously i'm going to take

play25:37

the accumulator and add the current

play25:39

value

play25:40

dot max

play25:42

and provide the initial value as 0

play25:45

and that's it i think this should do it

play25:47

let's find out 224 amazing we got the

play25:51

output so you see these type of

play25:53

questions can be asked to you during

play25:55

your javascript interviews and since

play25:56

you're watching this channel you're

play25:58

definitely able to crack them so hit the

play26:00

like on this video if you want me to

play26:01

create more such awesome javascript

play26:03

interview videos and the complete

play26:04

playlist for this javascript interview

play26:06

series will be in the description down

play26:08

below and obviously don't forget to

play26:09

subscribe or your code will give you

play26:11

errors

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
JavaScriptArray MethodsMap FunctionFilter FunctionReduce FunctionInterview PrepCoding TutorialPolyfillsDeveloper TipsTechnical InterviewCode Optimization
¿Necesitas un resumen en inglés?