Frontend Interview Experience (Unacademy) - Javascript and React JS Interview Questions

RoadsideCoder
13 Feb 202249:03

Summary

TLDRThis video script details a front-end developer's interview experience at an Indian unicorn company, focusing on JavaScript and React.js. The candidate shares the interview process, including four rounds, and discusses technical questions on topics like array functions, variable declarations, event delegation, and implementing debounce in React. The script also covers CSS box model concepts and creative ways to center a div. The final round delves into cultural fit and problem-solving approaches, offering insights into the candidate's thought process and preparation strategies.

Takeaways

  • 😀 The video discusses the interview process for a front-end developer role at an Indian unicorn company, focusing on JavaScript and React.js questions.
  • 🔍 The candidate contacted a hiring manager on Twitter with a resume, portfolio, and project links to secure the interview opportunity.
  • 📝 The interview consisted of four rounds, covering JavaScript, React.js, and HR/cultural fit, with the first two rounds dedicated to technical JavaScript questions.
  • 🛠️ The video provides detailed explanations and code examples for various JavaScript concepts, such as the differences between 'map' and 'forEach', 'null' vs 'undefined', and event delegation.
  • 📚 It covers how to approach common interview questions, like explaining the difference between 'let', 'const', and 'var', and the behavior of 'setTimeout' in loops.
  • 🤔 The interviewee shared their thought process and strategies for answering complex questions, such as implementing a custom 'Array.flat' method and understanding 'call', 'apply', and 'bind'.
  • 💡 Technical questions also included creating a 'compose' function for function evaluation and a polyfill for 'Promise.all', showcasing the candidate's ability to work with asynchronous JavaScript.
  • 🎨 Design-related questions tackled the CSS box model and various methods to center a div, reflecting the importance of CSS knowledge in front-end development.
  • 🔄 The concept of debouncing was introduced as a technique used in UI development to enhance performance, with a practical implementation example provided.
  • 👥 The final HR/cultural round focused on the candidate's approach to problem-solving, teamwork, and cultural fit within the company.
  • 📈 The video serves as a comprehensive guide for individuals preparing for front-end developer interviews, offering insights into technical and behavioral aspects of the process.

Q & A

  • What was the basis of the interview for the front-end developer role at an Indian unicorn company?

    -The interview was completely based on JavaScript and React.js questions.

  • How did the interviewee get the opportunity for the interview?

    -The interviewee contacted a hiring manager on Twitter, providing his resume, portfolio, and deployed projects along with source code links.

  • What was the interview process like for the front-end developer role?

    -The interview process consisted of four rounds, with the first two focusing on JavaScript, the third on React.js, and the fourth being an HR or cultural round.

  • What is the difference between the 'map' and 'forEach' array functions in JavaScript?

    -The 'map' function creates a new array with the results of calling a provided function on every element in the array, while 'forEach' performs the operation on each array element but does not return a new array.

  • What is the primary difference between 'null' and 'undefined' in JavaScript?

    -'null' is an actual value that represents no value, whereas 'undefined' means a variable has been declared but not initialized.

  • How does event delegation work in the context of an e-commerce site with a list of products?

    -Event delegation involves adding an event listener to a parent element and using it to handle events for child elements, avoiding the need to add individual event listeners to each child.

  • What is the concept of array flattening and how was the interviewer's question about it answered?

    -Array flattening is the process of transforming a nested array into a single-level array. The interviewee initially used the 'concat' and 'spread' operators to flatten one level and then wrote a custom 'flat' method to handle multiple levels of nesting.

  • What is the difference between the 'let' and 'const' keywords in JavaScript?

    -'let' allows for re-declaration and re-assignment within the same block scope, while 'const' cannot be re-declared or re-assigned and must be initialized at the time of declaration.

  • Can you explain the concept of 'closures' in JavaScript and how they were hinted to solve a problem in the interview?

    -Closures are functions that have access to the outer (enclosing) function's scope chain. The interviewer hinted at using closures to solve a problem related to printing output in a specific order using 'var' instead of 'let'.

  • What are the differences between the 'call', 'apply', and 'bind' methods in JavaScript?

    -The 'call' method calls a function with a specified 'this' value and arguments provided individually, 'apply' does the same but takes arguments as an array, and 'bind' creates a new function that, when called, has its 'this' keyword set to the provided value, with a given sequence of arguments preceding them.

  • What is the purpose of the 'Promise.all' method and how was the task of implementing its polyfill approached?

    -'Promise.all' takes an array of promises and returns a single promise that resolves when all of the input promises have resolved or rejects if any of them reject. The polyfill implementation involved using a 'for' loop to resolve each promise individually and collecting their results in an array.

  • How were React.js lifecycle methods explained in the context of class components and functional components?

    -For class components, methods like 'componentDidMount', 'componentDidUpdate', and 'componentWillUnmount' were demonstrated. For functional components, the 'useEffect' hook was used to replicate the same lifecycle behaviors based on the presence of dependencies in the hook's argument.

  • What are some common ways to center a div in CSS?

    -Three common methods mentioned are using 'top' and 'left' properties with 'transform', using 'flexbox' with 'justify-content' and 'align-items', and using 'display grid'.

  • What is the CSS box model and how does it relate to the properties of an HTML element?

    -The CSS box model is a concept that defines the rectangular boxes for elements using 'width', 'height', 'padding', 'border', and 'margin'. It represents the area that a block-level element occupies in the document.

  • What is debouncing and how was it demonstrated in the script?

    -Debouncing is a technique used to delay the execution of a function until a certain amount of time has passed without any new events being triggered. It was demonstrated by creating a debounced function that logs the input value to the console after the user has stopped typing for a specified delay.

Outlines

00:00

📝 Interview Experience at Indian Unicorn

The speaker details their journey of landing and going through an interview for a front-end developer position at an Indian unicorn company. They recount how they initiated contact with a hiring manager via Twitter, showcasing their resume, portfolio, and project source code. The interview process comprised four rounds, focusing on JavaScript, React.js, and HR/cultural fit. The speaker demonstrates coding examples in VS Code, starting with explaining the differences between 'map' and 'forEach' JavaScript array functions, and their various use cases.

05:02

🔑 JavaScript Concepts: 'null', 'undefined', and Event Delegation

The speaker explains the difference between 'null' and 'undefined' in JavaScript, emphasizing that 'null' is an actual value representing 'nothing', while 'undefined' indicates a declared but uninitialized variable. They also discuss the concept of 'event delegation' in JavaScript, illustrating how to add an event listener to a parent element to manage events for child elements, which is beneficial for performance and memory usage, especially on an e-commerce site with many product listings.

10:03

📚 Advanced JavaScript Questions: Array Flattening and Project Discussion

The interviewee is asked to flatten an array, initially explaining how to achieve one level of flattening using the concat and spread operators. They then proceed to write a custom 'flat' function that can handle arrays nested to a specified depth, demonstrating recursion. Following this, the speaker discusses their projects with the interviewer, explaining their tech stack choices and the reasoning behind using certain databases and front-end technologies.

15:04

🔄 Understanding 'var', 'let', and 'const' with setTimeout

The speaker discusses the differences between 'var', 'let', and 'const' in JavaScript, focusing on scoping rules and the ability to redeclare and reinitialize variables. They use a 'setTimeout' example to illustrate how these keywords behave differently, particularly highlighting the common mistake of expecting 'var' to have block scope leading to unexpected results when used inside loops.

20:06

🤝 JavaScript Function Manipulation with call, apply, and bind

The speaker explains the use of 'call', 'apply', and 'bind' in JavaScript for manipulating the context ('this' value) of functions. They demonstrate how 'call' and 'apply' can be used to invoke functions with a specified context and arguments, while 'bind' creates a new function with a preset context, allowing for partial application and delayed invocation of the function.

25:07

🎼 Compose Function and Promise.all Polyfill

The speaker is tasked with writing a 'compose' function, a utility often used in functional programming to evaluate a sequence of functions from right to left. They also implement a polyfill for 'Promise.all', explaining how it works by running multiple promises in parallel and resolving when all promises have settled. The custom implementation handles both successful resolutions and failures, aggregating results or rejecting early upon any promise failure.

30:08

🌀 React.js Lifecycle Methods and Component Centering Techniques

The speaker discusses React.js lifecycle methods, explaining 'componentDidMount', 'componentDidUpdate', and 'componentWillUnmount' in the context of class components, and how to replicate similar behavior in functional components using the 'useEffect' hook. They also address a common interview question about centering a div on a webpage, presenting three methods: using 'top' and 'left' with 'transform', utilizing 'flexbox', and employing 'css grid'.

35:09

📏 CSS Box Model and Debouncing Function in React

The speaker explains the CSS box model, detailing the properties of 'width', 'padding', 'border', and 'margin' that define the space an element occupies. They demonstrate how these properties are visualized in browser dev tools. Additionally, they introduce the concept of debouncing, a technique used to delay the execution of a function until a certain amount of time has passed without any new events, exemplified by search bars on e-commerce sites like Flipkart.

40:09

🏆 Concluding the Interview with HR and Cultural Fit Discussion

The speaker concludes the interview experience by discussing the final HR and cultural fit round, which focused on their approach to problem-solving, team dynamics, and cultural integration within the company. They encourage viewers to like, subscribe, and share the video, and express openness to creating more interview-related content based on audience feedback.

Mindmap

Keywords

💡Front-end Developer

A front-end developer is a software engineer who specializes in the client-side of web applications, focusing on user interface design and interactivity. In the video, the interviewee is discussing their experience interviewing for a front-end developer role at an Indian unicorn company, which is a privately held startup company valued at over $1 billion.

💡JavaScript

JavaScript is a high-level, interpreted programming language commonly used for enhancing web pages with interactive elements. The video script revolves around an interview process for a front-end developer role that heavily emphasizes JavaScript proficiency, as evidenced by the interview questions and the interviewee's use of JavaScript to demonstrate various concepts.

💡React.js

React.js, often simply referred to as React, is a popular JavaScript library for building user interfaces, particularly single-page applications. The interviewee mentions that the interview was based on React.js questions, indicating its importance in the front-end development field and its relevance to the job role they interviewed for.

💡Map vs. ForEach

The terms 'map' and 'forEach' refer to two different array functions in JavaScript. The interviewee explains that 'map' creates a new array by transforming each item of the original array, while 'forEach' iterates over the array without creating a new one. This distinction is important for understanding array manipulation in JavaScript, as demonstrated in the video.

💡Null and Undefined

In JavaScript, 'null' represents the intentional absence of any object value, whereas 'undefined' means a variable has been declared but not yet assigned a value. The interviewee discusses these concepts to clarify the differences between them, which is crucial for understanding variable states in JavaScript programming.

💡Event Delegation

Event delegation is a technique in JavaScript where an event listener is attached to a parent element instead of individual child elements. This approach is used to handle events for multiple elements efficiently. The interviewee explains this concept by demonstrating how to use event delegation to handle clicks on a list of products.

💡Array Flattening

Array flattening is the process of transforming a nested array (an array containing other arrays) into a single-level array. The interviewee is asked to flatten an array during the interview, showcasing their ability to manipulate arrays in JavaScript, which is a common task in front-end development.

💡Let, Const, and Var

In JavaScript, 'let', 'const', and 'var' are keywords used to declare variables. 'Let' and 'const' are block-scoped, meaning their scope is limited to the block in which they are defined, whereas 'var' is function-scoped. The interviewee discusses the differences between these keywords, highlighting the importance of scope in JavaScript.

💡Set Timeout

Set timeout is a JavaScript function used to delay the execution of a function or a code snippet until a specified amount of time has passed. The interviewee discusses a common interview question related to 'set timeout', explaining the output of a provided code snippet and the implications of variable scoping in JavaScript.

💡Promise.all

Promise.all is a JavaScript method that takes an array of promises and returns a new promise that resolves when all of the input promises have resolved, or rejects if any of them reject. The interviewee is asked to implement a polyfill for 'promise.all', demonstrating their understanding of asynchronous JavaScript and promise handling.

💡React Lifecycle Methods

React lifecycle methods are hooks into the lifecycle of a React component. They include methods like 'componentDidMount', 'componentDidUpdate', and 'componentWillUnmount'. The interviewee explains these methods in the context of class components and functional components, showing how to manage component behavior at different stages of its lifecycle.

💡CSS Box Model

The CSS box model is a fundamental concept in web design that describes the rectangular boxes that are generated for elements in the document tree. It includes properties such as width, height, padding, border, and margin. The interviewee discusses the box model, explaining how these properties combine to form the layout of HTML elements.

💡Debouncing

Debouncing is a programming practice used to ensure that time-consuming tasks do not fire so often, which can lead to performance issues. The interviewee demonstrates how to implement a debounce function in React.js, which is particularly useful for handling events like typing in a search bar, where you want to limit the rate at which a function is executed.

Highlights

Interviewee secured a front-end developer role at an Indian unicorn company through a cold contact on Twitter.

Interview process included four rounds with a focus on JavaScript, React.js, and HR/cultural fit.

The first round involved explaining the difference between JavaScript's map and forEach functions.

Null and undefined were distinguished based on initialization and type in JavaScript.

Event delegation was explained as a technique to add event listeners to a parent element rather than multiple children.

The interviewee demonstrated a custom array flattening method in JavaScript.

The let and const JavaScript keywords were compared in terms of scoping and re-declaration.

The output of setTimeout in a loop was discussed, highlighting the difference between var and let in scope handling.

Call, apply, and bind methods were explained for manipulating function context in JavaScript.

A polyfill for the JavaScript compose method was created to evaluate a sequence of functions.

The interviewee implemented a polyfill for Promise.all to handle multiple promises.

React.js lifecycle methods were explained for both class and functional components.

Multiple methods for centering a div in CSS were presented, including transform, flexbox, and grid.

The CSS box model was described, detailing width, padding, border, and margin properties.

A debouncing function was implemented in React.js to delay actions until typing has stopped.

The interview concluded with an HR and cultural fit round, focusing on problem-solving and team dynamics.

Transcripts

play00:00

a few months ago i interviewed for the

play00:02

role of front-end developer at an indian

play00:03

unicorn an academy the interview was

play00:06

completely based on javascript and

play00:08

react.js questions so that's why in

play00:10

today's video i'm gonna discuss the

play00:11

whole process on how i got the interview

play00:13

and all of the questions that were

play00:14

involved in the interview process so i

play00:17

contacted one of the hiring managers on

play00:18

twitter and provided him with all of the

play00:20

relevant stuff while cold contacting

play00:22

like my resume portfolio and my deployed

play00:25

projects along with the source code

play00:27

links and that is how i got this

play00:28

opportunity the recruiter got on a call

play00:30

with me the same day and he evaluated my

play00:33

projects and resume etc and decided to

play00:35

move forward with my application he

play00:37

explained me about the whole interview

play00:38

process including the number of rounds

play00:40

and what topics they are going to be

play00:43

so this interview process had four

play00:45

rounds first two being on javascript the

play00:48

third one on react.js and the fourth one

play00:50

was the hr or cultural round

play00:53

so let's go on and discuss the questions

play00:55

from the first round which was more of a

play00:57

screening round

play00:58

so i've opened vs code over here with

play01:00

html css and javascript file so when we

play01:03

go on and run this

play01:05

you're gonna see that you get a very

play01:06

important message over here

play01:08

now let's go back to our javascript

play01:10

files and start with our first question

play01:12

so the first question was what is the

play01:14

difference between map and for each

play01:17

so to explain this we're gonna take an

play01:19

array

play01:22

with let's say two five three four some

play01:26

random values now first of all what are

play01:28

map and for each these both are array

play01:31

functions to loop through the items of

play01:34

the array so if i go on and say a r r

play01:37

dot

play01:39

and we're gonna provide a callback over

play01:41

here so just like this

play01:45

and now we can perform any operation on

play01:47

this array so let's say return

play01:54

ar

play01:55

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

play01:57

going to return the whole array with two

play02:00

added to each item of the array it's not

play02:03

going to modify the original array

play02:05

whereas if we do this for each this

play02:08

performs like a normal for loop but this

play02:11

doesn't return anything just like map

play02:13

does so if i go on and say

play02:17

const map result

play02:21

and const

play02:23

for each

play02:25

result

play02:28

and go on and console.log both of these

play02:32

now let's go to our browser

play02:34

and open the console so we get an array

play02:37

in the result of the map so it returned

play02:39

us the array but didn't modify the

play02:41

original array so it returned us a new

play02:43

array with all of the items

play02:45

having a plus two

play02:47

but the for each didn't return us

play02:48

anything so both of these functions have

play02:51

a different use case let's say if we

play02:53

want to modify this array so what i'm

play02:55

going to do i'm going to take the index

play02:57

and i'll say

play03:00

so what this will do is it will modify

play03:02

the original array so

play03:04

let's say i'm going to add 3 to it and

play03:06

now i'll

play03:08

console log this array

play03:10

this was the array from the map and this

play03:12

was the modified array which was arr our

play03:15

original array now this was the first

play03:17

difference now the second difference is

play03:18

that you can chain stuff on map so i can

play03:22

say dot and something we can let's say

play03:25

if we want to do dot filter and do some

play03:28

operation inside this filter so we can

play03:30

do that with map but in forage since it

play03:33

doesn't return any array we cannot chain

play03:35

other methods after it so these two are

play03:37

the difference that came to my mind at

play03:39

that time so obviously there can be more

play03:40

differences as well if you know any

play03:42

other differences between map and forage

play03:45

let others know in the comments below

play03:47

alright so the next question was the

play03:48

difference between null and undefined so

play03:51

how i gave an answer for this is first

play03:52

of all i told the interviewer that null

play03:55

is an actual value but undefined means

play03:58

that the variable is declared but it's

play04:00

not initialized yet so let me show you

play04:03

so if we go on and console.log

play04:06

type of

play04:07

null

play04:08

you're going to see that the type of

play04:10

null is object but if we do the same

play04:12

thing with the undefined

play04:14

the type of undefined is undefined

play04:18

because this is not an actual value so

play04:20

if we go to the browser

play04:23

type off is not defined oops

play04:26

type

play04:26

off just like that yeah you see the type

play04:30

of null is object and type of undefined

play04:32

is undefined now a lot of people get

play04:34

confused between undefined and not

play04:36

defined so let me show you what that

play04:38

means

play04:39

so if i go on and let's say let a

play04:43

and i declared it just like this and let

play04:45

me remove both of these

play04:48

and i'm gonna console log a so you'll

play04:50

see that i get undefined inside of it

play04:54

just like this because it has been

play04:56

declared but the value is not

play04:57

initialized but we can go on and

play04:59

initialize it with a value

play05:01

which can be null so null can be an

play05:03

actual value unlike undefined so yep you

play05:06

see we get null over here but if we

play05:08

don't declare a at all then this will be

play05:10

not defined

play05:14

yep you see a is not defined now the

play05:16

follow-up question for this was the

play05:18

interviewer gave me these two things so

play05:20

he said null

play05:22

equals undefined now triple equals

play05:25

undefined and he asked me to tell the

play05:27

output for both of these statements so

play05:29

what do you think is going to be the

play05:30

output for this one so the first

play05:32

statement is going to be true and the

play05:35

second one will be false do you know the

play05:37

reason why because double equals just

play05:40

compares both of these entities without

play05:43

matching their types but this triple

play05:45

equals or the strict equality compares

play05:48

the types of both of these entities as

play05:51

well so this was more of a question

play05:53

related to double equals and triple

play05:55

equals so yeah now you know the answer

play05:57

the third question was to explain the

play05:59

even delegation

play06:01

so first of all let's understand what

play06:02

even delegation is so let's say if we

play06:05

have an e-commerce site with bunch of

play06:07

products listed on it and we want to

play06:09

perform an event when we click on a

play06:11

particular product so we are not going

play06:13

to obviously

play06:14

add the event listener on all of these

play06:16

products right on on each item of the

play06:18

list because that will fill our web page

play06:21

completely with these events and it's

play06:23

going to take a lot of memory so what

play06:25

even delegation does is we provide the

play06:27

event listener to the parent and access

play06:30

the child elements with the help of that

play06:32

event so let's go on and find out in the

play06:34

code i'm going to go to the index.html

play06:38

and below this

play06:39

below the h1 i'm going to add a simple

play06:42

div with id of products and i'm going to

play06:45

add a bunch of list items inside of it

play06:47

and let's see if this is rendered or not

play06:50

yup this is rendered

play06:52

now what i'm gonna do i'm gonna go

play06:53

inside the script

play06:55

js file and

play06:57

i'm gonna say document

play06:59

dot

play07:01

query selector and i'm going to select

play07:03

this products

play07:06

and then we're going to have an event

play07:07

listener of click event

play07:10

and a call back after it

play07:13

just like that we're going to take an

play07:15

event from inside of it and let's just

play07:17

console log this event and see what do

play07:19

we get when we click on it

play07:24

oops that's my bad i put the script tag

play07:26

over here

play07:28

i should have put the script tag right

play07:30

over here

play07:31

so now

play07:32

yep it's cleared anyway so if we click

play07:35

on any of these items

play07:37

you're going to see that we get this

play07:38

pointer event object and inside of it

play07:41

target

play07:42

inside of the target

play07:45

we're gonna have a tag name

play07:47

yup tag name of li so now we can target

play07:50

the li items inside of it and also you

play07:53

can see we get this text content

play07:54

offshored since we click on this shirt

play07:56

list item right below this i'm going to

play07:58

say if

play08:02

event dot

play08:04

target dot

play08:06

tag

play08:08

name

play08:08

is equals to

play08:10

li

play08:14

triple equals

play08:17

i mean window dot location

play08:22

dot href

play08:24

href will have

play08:26

this path

play08:28

so

play08:29

hash and let's say the name

play08:33

of the clicked product

play08:35

so event

play08:37

dot target dot id let's see now when we

play08:41

click on this this is going to route us

play08:43

to that page or like at least have the

play08:45

product name inside of the url so if you

play08:48

click on this yep you see we get this

play08:50

hash wallet so this is what even

play08:52

delegation is without adding event

play08:54

listener to each specific list item we

play08:56

just added it to the parent and we are

play08:58

using it to click the other list items

play09:01

now the next question that the

play09:02

interviewer asked me was he provided me

play09:04

with this array and he asked me to

play09:06

flatten the array so what does

play09:07

flattening an array means so when we

play09:10

flatten the array it's gonna look

play09:11

something like this without these nested

play09:14

array inside of it so first of all he

play09:16

asked me to flatten the array just one

play09:18

level so there can be multiple levels

play09:20

right so we can have an array inside of

play09:22

another array so stuff like this so

play09:26

he asked me to flatten this to just one

play09:28

level so what i did was

play09:31

i simply

play09:32

let flattened

play09:36

and

play09:37

i added an empty array dot concat

play09:41

and just spread this array inside of it

play09:44

so

play09:45

ar

play09:46

and what this will do is it's going to

play09:48

spread this array and then concat it

play09:51

with this empty array which will result

play09:52

in one level of flattening so if we go

play09:56

on and console log this

play09:59

and check

play10:01

you're going to see we get a flattened

play10:03

array but this is not flattened

play10:04

obviously because this was nested inside

play10:07

of this one so

play10:08

it's fair

play10:10

it worked as expected but then

play10:12

interviewer asked me to write the custom

play10:15

array flat method so if you don't know

play10:17

there is a method inside of javascript

play10:19

called array.flat

play10:21

so if you go on and say um let me remove

play10:24

this arr dot

play10:25

flat

play10:27

and now you can see in the browser it

play10:30

flattened the complete array okay so we

play10:32

are supposed to give this depth as well

play10:33

so let's say we want to flatten this

play10:35

array to the two levels because

play10:36

obviously there are only two levels

play10:38

inside of this so if i put two over here

play10:40

yep you see the whole array has been

play10:42

flattened so we are supposed to write a

play10:44

custom flat function for our array so

play10:48

let's

play10:49

remove this for now and

play10:52

add a function called custom

play10:55

flat

play10:57

this takes an array

play10:58

and depth

play11:01

which by default will be one now inside

play11:03

this to store the result we're going to

play11:05

have a result

play11:08

array an empty result array and inside

play11:10

of that i'm gonna

play11:12

for each through this

play11:17

now here i'm gonna check if the provided

play11:20

item is an array or not so i'm gonna say

play11:23

array

play11:24

dot

play11:26

is array so this checks if the provided

play11:29

atom is arrow or not basically and i'm

play11:31

going to check if the depth still

play11:33

remains if we are supposed to go

play11:36

further inside of this array so i'm

play11:37

going to check if depth

play11:39

is more than

play11:41

zero so i'm going to explain don't worry

play11:43

this will actually be a recursion

play11:45

technique so

play11:46

we're going to call this custom flat

play11:48

again inside of it so what we'll do over

play11:50

here is i'm going to say result

play11:53

dot push

play11:55

whatever we get from this custom

play11:58

flat

play12:00

and we're gonna provide it this

play12:01

remaining array

play12:04

i'll explain this again don't worry

play12:07

and we're gonna remove the depth

play12:10

by 1. otherwise if this is not an array

play12:12

then obviously this is going to be a

play12:14

single number so this is already been

play12:16

flattened right so

play12:18

i'm going to say else

play12:21

result dot push

play12:25

ar

play12:26

and finally below this i'm gonna return

play12:29

result

play12:31

okay so let's see what's going on over

play12:32

here

play12:33

when we go on and console.log

play12:36

custom flat and we provide it with our

play12:39

array and depth uh let's say two and see

play12:42

if this works or not yep this is working

play12:44

as expected if i give this one or let's

play12:47

say if you don't give the depth

play12:49

uh yep it flattened it by one level okay

play12:53

so let's see what's going on over here

play12:54

we we have a result array which is empty

play12:57

inside this we are mapping through the

play12:59

complete array that is this array right

play13:00

here so first of all it's going to

play13:02

encounter one array so it's gonna check

play13:05

if this is an array

play13:06

yup it's an array and it's depth more

play13:08

than zero

play13:09

yep by default it was one if we haven't

play13:11

provided depth

play13:12

so it was more than zero yep that's fine

play13:16

so we went in and did result dot push

play13:19

now it went again inside of it and this

play13:21

time

play13:22

array is this one this is the array and

play13:24

depth is zero this time

play13:27

right so now it went again inside of

play13:29

over here array.forage and

play13:31

it mapped through this one and two so

play13:34

first of all it was one so it checked if

play13:36

this is an array no this is not an array

play13:38

so it went over here else result dot

play13:40

push err so it pushed one into the

play13:44

into this array so one then comma again

play13:47

it looped through this item to it was

play13:49

not an array again so it posed it again

play13:51

inside of it so two

play13:54

so it returned this complete object now

play13:56

obviously this uh array is emptied so

play13:58

it's going to return this result so it

play14:00

returned this one comma 2 and we spread

play14:02

it inside of result.push so this result

play14:05

was the parent result the original

play14:08

result where we were adding our answer

play14:10

so it got added 1 comma 2.

play14:13

now then again this is going to be the

play14:14

same case with 3 comma 4 and it's going

play14:17

to add 3 comma 4 inside of it so

play14:19

similarly it's going to go much deeper

play14:22

as it encounters more and more arrays

play14:24

and flattens the complete array until

play14:26

the job is done so yeah i hope

play14:28

i was clear enough to explain this

play14:31

custom flat function so let's move on to

play14:34

our next question

play14:35

so next interviewer asked me to show him

play14:37

all of my projects so i already had

play14:40

shared my portfolio with him so i'm

play14:42

gonna go to my portfolio over here

play14:45

which is this one

play14:48

looks good isn't it the tutorial for

play14:49

this portfolio site is coming soon on

play14:51

our channel so make sure you're

play14:52

subscribed to the channel with bell

play14:53

notifications turned on

play14:58

so i showed him all the projects one by

play15:00

one and he liked this project a lot the

play15:02

chat app with mindstack the tutorial for

play15:04

which i've already created on my channel

play15:06

so you can click the link in the

play15:07

description or i button above to learn

play15:09

how you can also build this project with

play15:10

monstack so he asked questions like why

play15:13

did you use a particular tech stack why

play15:15

did you use this database and this

play15:17

front-end technology and stuff like that

play15:20

and that was it for round one

play15:22

so for the first question of round two

play15:24

the interviewer asked me the difference

play15:25

between where let and const keywords so

play15:28

first thing that i could think of was

play15:30

that where is functional scoped and

play15:33

let and const are block scoped so what

play15:36

does that mean

play15:37

let's go and find out so if i create a

play15:40

block over here

play15:42

and

play15:43

let's have a var equals um

play15:45

let's say hello

play15:47

and if i try to console log it

play15:51

to check if the scope belongs outside of

play15:53

this block or not so yep we get this

play15:56

hello over here but what if we change

play15:58

this to let

play16:01

it says a is not defined

play16:04

but what if we change it to const

play16:07

it's again going to say that a is not

play16:09

defined it's because the scope of these

play16:12

const and let keywords is only inside of

play16:14

this these two curly braces so if i

play16:17

let's say move this inside of over here

play16:20

yup we get this hello but

play16:22

that is not the case with where

play16:24

now the next difference that i came up

play16:26

with was related to declaration

play16:28

so if i remove this and let's say right

play16:32

where a

play16:33

equals

play16:34

five and now if i go on and say where a

play16:38

again

play16:39

equals 10

play16:41

then that's completely fine it's not

play16:42

going to give us any error

play16:44

but

play16:45

if we write let equals a and we try to

play16:48

redeclare that

play16:51

we're gonna get this error that

play16:53

identifier a has already been declared

play16:56

and the same case is with const as well

play17:01

yep identifier a has been already

play17:02

declared but what about initialization

play17:05

let's check let's find out

play17:07

so we've already seen that where

play17:09

obviously can be initialized twice so

play17:12

that's not a problem but if we make this

play17:15

let and let me remove let from over here

play17:17

since it cannot be declared twice so can

play17:19

let be initialized again let's see

play17:22

yes it can be initialized it doesn't

play17:23

give us any error but what about const

play17:29

assignment to a constant variable this

play17:31

gives us an error so const cannot be

play17:34

re-initialized

play17:36

it cannot be re-declared or initialized

play17:38

but let can be re-initialized now one

play17:41

more major difference is that if we

play17:43

remove this

play17:44

value from here and let's say where a

play17:48

then it can be declared without adding a

play17:51

value inside of a right yep we can do

play17:53

that

play17:54

can we do that with let

play17:57

yup we can do that as well but can we do

play18:00

that with const

play18:03

it gives us an error missing initializer

play18:05

in the cons declaration so you cannot

play18:08

declare const like this you have to

play18:10

provide a value to it

play18:11

so nowhere right now the second question

play18:14

for round two was an output based

play18:16

question on set timeout so the interval

play18:18

provided me with this code snippet and

play18:20

obviously we're gonna run this over here

play18:22

so he asked me what is going to be the

play18:24

output for this

play18:26

so let's think about it most people will

play18:28

think that since we are console logging

play18:31

over here we are running a loop over

play18:33

here for loop and we are console logging

play18:35

each of these values inside of over here

play18:37

after one second each so most people

play18:39

will think that the output is going to

play18:41

be this 0

play18:43

1

play18:44

2

play18:45

is it let's find out

play18:49

it is 3 3 times let's refresh the page

play18:53

so 3 3 and 3 after one second each so

play18:56

why is this happening this is because of

play18:59

where

play19:00

so what where does this we just

play19:02

discussed in our previous question

play19:04

so where doesn't have a block scope

play19:06

where has a function scoped so what's

play19:09

happening over here is

play19:11

first time this runs

play19:13

where value is zero but this is not

play19:16

going to be printed right away because

play19:18

set timeout only runs after the complete

play19:20

code has ran successfully right so this

play19:23

is not going to run so we have a

play19:24

reference to that i variable in our

play19:26

memory so we have a reference over here

play19:29

let's say

play19:30

okay then again the i is one

play19:33

then we again have a reference to the i

play19:36

variable

play19:37

just like that and then i is 2 then we

play19:40

again have a reference to i variable and

play19:42

then the third time the value of i is 3

play19:45

but if we obviously we won't go inside

play19:47

of over here since it says that i has to

play19:49

be less than 3 so we're not going to run

play19:51

inside of it we come out of this loop

play19:53

and then

play19:54

set timeout has finished its time and

play19:57

now our

play19:58

js engine is going to print all of these

play20:00

values so last time the js engine

play20:02

encountered the value of i it was 3. now

play20:05

here js engine is referring to the

play20:07

current value of i so the current value

play20:09

of i was 3 so it's going to print 3 3

play20:12

and 3

play20:13

just like that

play20:15

now what's the solution for this

play20:18

you may have guessed it right the

play20:20

solution for this is using a let instead

play20:23

of where

play20:25

let's see the output

play20:26

0 1 2 yes so what's happening over here

play20:30

is let is block scoped so first of all

play20:33

let's value was zero so it has the

play20:36

completely different scope so in this

play20:38

scope the value of i was zero so it's

play20:41

gonna print it later on

play20:43

then again

play20:45

the second value of i was one so again

play20:49

after the complete code has run

play20:51

successfully it's going to look into the

play20:53

scope of that i variable and when it

play20:55

looks into all the three scopes it's

play20:57

going to find 0 1 and 2. so this is the

play21:00

logic behind this question so actually

play21:02

this was the follow-up question that the

play21:04

interviewer asked i just explained it to

play21:06

you in one go so these were two

play21:08

different questions that the interviewer

play21:09

asked when we asked first to tell him

play21:11

the output of the first with where and

play21:13

then he asked me how we can print 0 1 2

play21:16

so then i went on and explained him with

play21:18

let

play21:19

so this was the question but few of the

play21:21

interviewers extend this question even

play21:23

more they're going to say you're not

play21:25

allowed to use let you're going to have

play21:27

to use where instead of lit and you have

play21:30

to

play21:31

print 0 1 2. you have to give this

play21:34

output

play21:35

how are we going to do that so this is a

play21:37

task for you all

play21:39

i'm going to give you a small hint it's

play21:40

going to use closures all right pause if

play21:43

you're not yet following me on twitter

play21:45

go to twitter.com push underscore eon or

play21:47

click the link in the description down

play21:49

below and hit that follow button right

play21:51

now

play21:52

i'm waiting for you

play21:55

i'm still

play21:56

waiting fine let's continue with the

play21:59

video now the third question was to

play22:01

explain call apply and bind so with call

play22:04

apply and bind methods you can basically

play22:06

manipulate the context for a particular

play22:08

function so let me go on and show you so

play22:11

i went ahead and created this object

play22:13

over here where person which contains a

play22:15

name variable and a function called

play22:18

hello which is console.logging this dot

play22:20

name says hello and thing which will

play22:22

provide inside of this function so let's

play22:24

go on and just uh let's uh

play22:27

invoke this function person.hello

play22:30

and i'm gonna let's say provide a world

play22:33

over here

play22:34

let's see what do we get

play22:36

we get roadside coder says hello world

play22:39

yep that's fair enough because this

play22:41

this keyword is pointing to this person

play22:44

object over here now let's see how call

play22:46

works but before that i'm going to

play22:48

create another object over here with

play22:50

name pure chagawal so now what i'm going

play22:53

to do i'm going to say hello dot

play22:56

call

play22:57

and i'm going to provide this this

play23:00

object

play23:03

so what call does is

play23:05

call takes an object which is going to

play23:07

become the context for this particular

play23:09

function

play23:10

and for the other parameters it takes

play23:13

the arguments that this function is

play23:15

going to take so i've provided the world

play23:16

over here so now this should print

play23:19

this name instead of this name

play23:21

let's see

play23:22

yep that's perfectly fine

play23:26

now how is the apply different from call

play23:28

so there's a very small difference so we

play23:30

can say apply and instead of providing

play23:34

params like this we just need to provide

play23:36

them inside the array

play23:38

and now over here this is going to be

play23:39

provided to this thing over here and

play23:42

let's see what do we get

play23:43

yep this is what we were expecting now

play23:46

what does bind do so bind doesn't take

play23:49

this second param

play23:51

it just takes a context and it returns

play23:54

us a completely new function with this

play23:57

context so i'm gonna say const new

play23:59

person

play24:02

and now this can be invoked

play24:06

just like this

play24:08

and you're gonna see uh oh okay we

play24:11

didn't provide it the

play24:13

argument which was this so we can say

play24:17

world

play24:18

yep that's that's what you were

play24:20

expecting

play24:22

yep so this was the difference between

play24:23

call apply and bind now the next

play24:26

question was on infinite carrying so if

play24:28

you have seen my previous video on this

play24:29

front-end interview series i was asked

play24:31

the same question in the cast 24

play24:33

interview so you can go ahead and watch

play24:35

that video there i've explained this

play24:37

question in a more in-depth way so link

play24:39

will be in the description or i button

play24:41

above so you can go and watch that video

play24:43

now for the fifth function the

play24:45

interviewer gave me these three

play24:46

functions right here and he told me to

play24:48

write a compose method basically a

play24:50

polyfill for the compose method which

play24:52

will evaluate all of these three

play24:54

functions so if you don't know what

play24:56

compose is basically what compose does

play24:58

is

play24:59

let's say if i write

play25:01

const evaluate

play25:05

there's going to be a compose function

play25:07

oops

play25:10

which is going to take all of these

play25:12

functions right here so let's say add 5

play25:15

subtract 2

play25:18

multiply 4 so it took all of these three

play25:20

functions and then it's going to return

play25:22

another function which will take an

play25:24

initial value

play25:25

so i'm going to console.log evaluate and

play25:28

it's gonna take initial value let's say

play25:29

five so what this is going to do is it's

play25:32

gonna take five and first of all it's

play25:34

gonna send five to this multiply four so

play25:37

five into four is twenty

play25:39

now that one the result of this function

play25:41

is going to be sent to this subtract 2

play25:43

so 20 minus 2 is going to be 18 and the

play25:46

result of this that is 18 is going to be

play25:48

sent to add 5 which is going to be 23

play25:51

over here and then it's going to be

play25:52

returned

play25:53

to us here and we are going to console

play25:56

log 23. so this is the function that we

play25:58

are supposed to write over here so let's

play26:00

see how we can do that so i'm gonna

play26:02

comment this out for now and i'll say

play26:04

const

play26:06

compose

play26:08

so now what's the first thing that we

play26:10

are supposed to do

play26:11

we're supposed to take all of these

play26:12

functions right so all of these

play26:14

functions so i'm just gonna we don't

play26:16

know how many functions the user is

play26:17

going to supply us so i'm just gonna say

play26:20

functions

play26:22

yup so this is going to take the array

play26:24

of all of these functions

play26:26

now inside of it

play26:27

i'm going to return another function

play26:30

and this will take arguments for that

play26:32

function for these functions and now

play26:34

instead of this is going to be the main

play26:36

logic of this compose

play26:37

we're going to take all of these

play26:39

functions

play26:40

and we're going to reduce right so there

play26:43

there are two types of such functions

play26:45

one is compose and the other is pipe so

play26:48

what pipe does is pipe evaluates from

play26:50

left to right but compose evaluates from

play26:53

right to left so i'm going to say

play26:55

functions dot reduce

play26:58

right

play27:00

so we're going to reduce it from right

play27:01

to left so it's going to take two things

play27:04

first is going to be the argument which

play27:07

was received from the previous function

play27:08

and a function the actual current

play27:11

function and i'm gonna say function and

play27:14

provide that argument to that function

play27:17

and it's going to have an initial value

play27:18

which will be rx so if you can see we

play27:21

have provided an initial value over here

play27:22

so this is going to be rx just like that

play27:26

um i believe this should work let's see

play27:31

this should give us 23.

play27:33

we're getting undefined why is that

play27:36

oh it's because we're supposed to return

play27:38

over here

play27:40

yep

play27:41

let's see yeah we get 23 as an output

play27:44

so yeah this is what the compose

play27:46

function is this is a really important

play27:48

interview question and i would recommend

play27:49

you to go and try the pipe as well now

play27:52

the sixth and the last question for this

play27:54

round was to implement promise.all now

play27:56

what is promise.all let's see so before

play27:59

uh explaining promise.all i'm going to

play28:01

create a few promises

play28:03

let's say first is going to be show text

play28:05

which will show the text after given

play28:07

number of time

play28:09

and

play28:10

let's just implement this now so so

play28:13

promise dot all takes a bunch of

play28:15

promises so first of all let's say show

play28:18

text

play28:20

and this is going to take a text like

play28:22

hello

play28:24

and a time which i'm gonna say let's say

play28:26

one second

play28:27

now the second promise that i'm gonna

play28:29

give is promise dot resolve

play28:32

resolve

play28:34

and let's say hi

play28:37

oh also this is going to be

play28:40

an array oops

play28:42

both of these promises are going to be

play28:43

an array it takes an array of promises

play28:45

and then we're going to add dot then

play28:50

let's say value and whatever the value

play28:53

that we get

play28:54

so what this basically does is it takes

play28:56

a bunch of promises

play28:57

and it resolves all of them if all of

play28:59

them are resolved it's going to return

play29:01

an array with all of the resolved result

play29:04

so in this case we are going to get

play29:06

hello and high yup just like this

play29:09

so it waited so let me show you so if i

play29:12

refresh the page it's gonna wait for all

play29:14

of the promises to get completed so it

play29:16

waited one second for this to get

play29:18

completed and then it printed both of

play29:20

these promises together but what if one

play29:22

of these promises fail

play29:24

so i'm gonna say promise dot reject

play29:27

buy

play29:30

then what's going to happen then all of

play29:32

these promises are going to fail any one

play29:34

of these promises fails the complete

play29:37

promise.all is going to get failed

play29:39

so let's create the polyfill for this

play29:42

i'm going to create a function

play29:47

my promise

play29:49

all

play29:51

and

play29:53

it's going to take a bunch of promises

play29:56

as

play29:56

the arguments

play29:58

inside this i'm going to have a result

play30:01

array it's going to be empty which will

play30:03

contain the result for all of these

play30:05

promises at the end now i'm gonna return

play30:08

a new promise

play30:12

which will take resolve

play30:15

and reject

play30:17

just like always

play30:22

now inside this i'm gonna for reach

play30:23

through all of these promises

play30:25

so promises the promises that we receive

play30:27

over here promises dot

play30:30

for each

play30:31

and now for each promise so i'm gonna

play30:33

take a single promise over here along

play30:35

with the index

play30:36

i'm gonna do some things

play30:40

so i'm gonna resolve each of the

play30:41

promises so promise dot then

play30:45

and i'm gonna take the result

play30:47

and if this gets successfully resolved

play30:49

i'm gonna add the result

play30:52

to our result array so result.push

play30:55

is going to be um let me rename this to

play30:58

res

play30:59

i think that's better

play31:01

so res

play31:02

i'm gonna add this to our result array

play31:04

and now i'm going to check if all of

play31:06

these promises are resolved now so i'll

play31:09

say if

play31:10

index

play31:11

is equals to

play31:14

promises

play31:16

dot length minus 1 oops

play31:19

length

play31:20

minus 1. so if this is all of the

play31:22

promises are resolved so i'm going to

play31:23

resolve the complete thing

play31:25

so resolve oops

play31:29

resolve

play31:31

the result

play31:36

just like that but if any of any one of

play31:39

these have failed i'm gonna check

play31:41

so this was dot then and after this

play31:44

is going to be dot catch

play31:47

if any one of these promises have failed

play31:50

then i'm going to instantly fail it and

play31:52

i'm going to reject

play31:55

with the error

play31:56

just like that

play31:57

um i believe this should be it let's

play32:00

let's find out

play32:01

i'm gonna provide it with my promise all

play32:03

and let's see if this works or not so

play32:05

this should fail

play32:06

with yep as expected it should fail but

play32:09

what if we remove this failing condition

play32:12

this should give us hello and high

play32:14

yup we get both of these result in our

play32:17

array so yeah that was what the

play32:20

interviewer was expecting from me and

play32:22

this concludes our round two of the

play32:23

interview process

play32:25

so the third round was on react.js so

play32:28

the first question for this round was to

play32:30

explain the lifecycle methods in

play32:32

react.js both by using class components

play32:35

and functional components so i went on

play32:37

to explain it with the class component

play32:39

first so

play32:40

let's let me move this function right

play32:42

here

play32:43

and i'm gonna add a class app

play32:47

extends

play32:49

react dot

play32:50

component and inside of this class i'm

play32:53

gonna have let's say a render so now the

play32:56

first life cycle method is component

play32:59

dead mount

play33:00

so what does component mount do

play33:02

so let me write it componented mount

play33:05

and component mount basically runs when

play33:08

our app is rendered for the first time

play33:10

when our component is rendered for the

play33:12

first time so i'm gonna console.log over

play33:15

here

play33:16

component dead mount oops

play33:19

component mount runs

play33:21

so let's see

play33:22

let's open the console

play33:24

and you can see the component mount runs

play33:26

for the first time

play33:27

so basically we use component in mount

play33:29

for use cases like when we want to fetch

play33:32

the data from an api or let's say when

play33:34

we supposed to provide some initial

play33:35

value to some state and stuff like that

play33:38

then there is component date update so

play33:41

for that um let's say

play33:44

let's i'm going to create another

play33:45

component over here

play33:47

so

play33:48

create a new directory called components

play33:52

and inside of it i'm going to create a

play33:54

new file called let's say

play33:57

counter dot js

play33:59

and this counter is going to have this

play34:01

simple class component with a number

play34:04

prop that i'm going to send to it so

play34:06

what i'm going to do here is i'm going

play34:07

to create a local state

play34:11

so state

play34:13

and this will have a num of zero state

play34:16

when we click on this button it should

play34:18

increment this state let me first of all

play34:21

import that component over here so i'm

play34:23

gonna add

play34:26

some fragments and inside of it i'm

play34:28

gonna import

play34:30

the counter

play34:32

just like that and it's gonna take this

play34:34

num

play34:35

so this counter had which prop

play34:38

number prop

play34:40

so i'm gonna provide number

play34:43

to be this dot state dot num

play34:47

so it's obviously zero times right now

play34:49

when we click on this button it should

play34:51

increment this so let me create a handle

play34:53

click function for this so what this

play34:55

handle click will do this will set our

play34:57

state to plus one

play34:59

so let me invoke this real quick over

play35:01

here so on click

play35:04

this dot

play35:05

and click dot bind

play35:08

this

play35:09

so this should work let's try it out yep

play35:12

this is working cool now let me

play35:14

demonstrate component did update

play35:17

so if i go inside this counter

play35:19

so component did update takes a few

play35:22

things first will be previous

play35:25

props

play35:26

and previous state so that we can

play35:29

compare

play35:30

our new state with the previous state or

play35:33

new props with the previous props so i'm

play35:35

sending this number over here right i'm

play35:37

going to say whenever this number

play35:39

changes that is whenever this prop gets

play35:41

updated do something basically do a

play35:44

console log or something like that so

play35:45

i'm going to say if

play35:48

previous props dot

play35:51

number

play35:52

is not equals to the current

play35:55

that is this dot props dot

play35:59

number

play36:01

then let's say let's console log

play36:04

componented update

play36:06

runs

play36:07

so yeah let's see

play36:10

if we refresh our app

play36:15

component in mount runs okay fine if we

play36:18

click on increment oh you know what i'm

play36:19

what i'm going to do i'm just going to

play36:21

bring this

play36:22

in this component itself so that you can

play36:24

see the life cycle of this component

play36:30

yeah

play36:31

so

play36:32

i'm gonna remove this component now

play36:34

now notice

play36:36

when i render this component let me

play36:38

clear this console when i enter this

play36:40

component it's gonna render component

play36:43

mount runs but whenever the prop changes

play36:46

you're gonna see component it update

play36:48

runs every time every single time so

play36:51

this is what componented update does

play36:53

whenever the props gets updated or state

play36:56

gets updated

play36:58

now there's a third lifecycle method

play37:00

which is component will unmount so

play37:02

component

play37:04

will unmount so this will basically run

play37:06

when the component is removed from our

play37:07

app or when basically when it's

play37:09

unmounted

play37:10

so um

play37:12

runs

play37:13

okay cool

play37:15

so

play37:17

whenever we do anything this doesn't run

play37:19

at all but when we remove this component

play37:21

you're gonna see

play37:22

component will unmount runs yep so these

play37:25

three were the lifecycle method

play37:26

obviously there are more lifecycle

play37:28

methods but these three are the main

play37:30

major lifecycle methods in react these

play37:32

basically define the complete life cycle

play37:35

of a particular component from mounting

play37:37

to updating to getting unmounted

play37:40

okay so now let's see how we can

play37:42

implement this by using functional

play37:44

component

play37:45

so i'm gonna go inside of over here i'm

play37:47

gonna

play37:48

remove this counter class and i'll just

play37:52

say function

play37:55

counter

play37:57

and basically create a simple functional

play37:59

component and this takes a number prop

play38:02

and i'm going to return

play38:08

cool

play38:09

so whoop

play38:10

my bad

play38:12

so this is what a simple functional

play38:13

component is if we go and increment this

play38:15

yep it runs

play38:17

so how do we use lifecycle methods

play38:19

inside of it so to use lifecycle methods

play38:22

inside of a functional component we use

play38:24

a hook called use effect

play38:27

so use effect takes a callback

play38:31

and has our

play38:34

array of dependencies so first of all

play38:37

when we write anything inside of this

play38:38

with the array of dependency as empty

play38:42

is it component

play38:45

yeah let's um let's clear this console

play38:49

and mount this component again so if i

play38:51

remove this and mount this again you see

play38:54

component is mounted it's going to be

play38:55

run just one single time because the

play38:57

array of dependency is empty we click on

play39:01

increment nothing happens

play39:02

cool so how do we update this

play39:05

whenever we so if we write anything

play39:07

inside of over here so let's say if we

play39:08

write number inside of over here

play39:10

whenever the value of number changes

play39:12

this is going to run so i'm just going

play39:15

to say component is updated

play39:20

let's see

play39:22

yep you can see the updated values going

play39:24

up

play39:25

number of times it's getting updated

play39:26

it's going up yep that's that's what we

play39:28

were expecting

play39:30

so this is component dead update with

play39:32

functional component now how do we

play39:34

implement component will unmount with

play39:36

functional components so for unmounting

play39:38

we just say

play39:40

return um return

play39:43

and we return an empty function

play39:46

with

play39:48

let's have a console log here

play39:52

this is unmounted

play39:56

let's see if this works or not let me

play39:57

clear this up

play39:59

and now if i

play40:00

go on and remove this

play40:02

you're gonna see this is unmounted so

play40:04

this is how you implement all of the

play40:06

lifecycle methods with the help of

play40:08

functional components in react

play40:10

now the next question for this round is

play40:12

one that is asked a million times in

play40:14

these web developer interviews are you

play40:17

ready

play40:17

drum roll please

play40:21

ways to center a div yes this was the

play40:24

question that was asked in this round

play40:26

and i was never more prepared for a

play40:28

question than this question i was

play40:30

waiting my whole life for the

play40:31

interviewer to ask me this question so

play40:33

let's go on and find out how many ways

play40:35

does roadside coder know to center a div

play40:38

so i'm gonna go and open the html file

play40:40

and over here let's say inside of this

play40:43

body i'm gonna have a div

play40:46

and inside of the div i'm gonna just put

play40:47

this h1 tag

play40:49

let's go inside of the style.css

play40:52

um you know what i'm gonna put it right

play40:54

over here

play40:55

i'm just gonna say div

play40:57

and let me just give it some background

play41:00

color for now

play41:02

let's give this color probably so for

play41:06

body i'm gonna do

play41:08

is this

play41:09

width

play41:10

to be hundred percent

play41:13

and

play41:14

height

play41:17

height to be hundred percent

play41:18

okay cool now to center this div three

play41:21

ways came to my mind first one was by

play41:23

using top and left

play41:25

so um let's say top fifty percent

play41:30

and left

play41:32

fifty percent

play41:34

oh and i'm gonna give position to be

play41:36

absolute

play41:39

now to align it back to the center i'm

play41:41

gonna say transform

play41:45

translate and i'm going to translate

play41:47

back by 50

play41:50

on both

play41:51

axis

play41:53

oh i mean mine is 50

play41:57

[Applause]

play42:01

second method

play42:02

to center our div will be by using

play42:04

flexbox

play42:05

so i'm gonna remove all of this thing

play42:08

and i'm gonna give it display

play42:10

flex

play42:12

yeah

play42:13

now since we have given a display flex

play42:16

i'm gonna give it justify content to

play42:18

center

play42:19

so it gets centered

play42:21

at the middle horizontally centered and

play42:24

then i'm gonna vertically center by

play42:26

align items center

play42:29

and this doesn't work because okay

play42:31

height i'm going to give

play42:33

100 vh

play42:34

yup this works

play42:36

great let's jump on to the third method

play42:38

and the third method is by using

play42:40

display grid so instead of display flex

play42:43

i'm just going to add display grid

play42:45

and it's not going to make any

play42:46

difference so if i comment this out

play42:48

oh okay but we're not concerned with

play42:50

that so yeah so it we have centered it

play42:54

successfully obviously they are there

play42:56

can be more methods to center a div but

play42:59

like these three were the methods that i

play43:00

could remember at that time now the next

play43:02

question for this round was what is css

play43:06

box model

play43:07

so let's find out

play43:08

so since we have created this div

play43:10

already i'm going to reuse this

play43:12

so this div has

play43:14

let's give this div some width

play43:17

of

play43:18

i don't know 200 pixel

play43:21

oh

play43:22

400

play43:23

ah this is fine yeah four in pixel i'm

play43:26

gonna give it some border

play43:29

so border let's say five pixel

play43:32

solid

play43:33

black

play43:35

okay i'm gonna give it some padding

play43:38

of 20 pixels

play43:40

and some margin

play43:44

of 20 pixel

play43:45

okay so these four are the properties

play43:49

which combine to make the css box model

play43:51

every single html element has these four

play43:53

properties right so these four combine

play43:56

to form a box element let me show you

play43:57

that visually so if you go to inspect

play44:03

and this div is selected right yeah this

play44:05

div

play44:06

so

play44:07

over here in the computed you can see

play44:10

these four properties so we gave it some

play44:12

width of 400 pixels some padding of 20

play44:15

pixels some border of 5 pixels and some

play44:17

margin of 20 pixels so these four

play44:19

combine to form the box model for our

play44:22

html element

play44:24

now the fourth and the last question of

play44:26

this round was to implement a d-bounce

play44:28

function in react.js

play44:30

so what is d-bounce let's go and see so

play44:33

if i go on and open

play44:35

flipkart.com

play44:38

so flipkart is an e-commerce website so

play44:41

instead of here you can see there's a

play44:42

search bar

play44:43

right if you go on and search

play44:46

let's say dslr camera

play44:49

did you notice something

play44:51

when i stopped typing after a few

play44:54

millisecond the result came so if i

play44:56

let's say remove this just when i stop

play44:58

typing only then these results get

play45:00

updated so let's say if i type hats

play45:04

see or let's say jackets

play45:07

after i'm done writing jackets it

play45:10

fetches all of these results so this is

play45:12

called debouncing in debouncing when we

play45:15

write something inside the search only

play45:16

after we have stopped typing only after

play45:19

a few milliseconds or however time which

play45:21

we want to keep inside of the debounce

play45:24

it's going to perform some action i'm

play45:26

going to close this now and

play45:28

i'm going to create an input tag here

play45:39

and whenever we type inside this input

play45:40

tag i want only after one second when

play45:42

i'm stopped typing it should print in

play45:44

the console so

play45:47

i'm gonna

play45:49

go over here and create

play45:51

const

play45:52

my d bounce

play45:56

now this d bounce take two things first

play45:59

thing is going to be a function or a

play46:00

callback and second thing is going to be

play46:02

a delay

play46:05

so i'm gonna first set a timer over here

play46:08

let timer

play46:09

and then i'm going to return a function

play46:12

which is the debounced version of the

play46:14

function

play46:15

that we got

play46:17

and sort of this first of all i'm gonna

play46:18

check

play46:19

if there's something inside of the timer

play46:21

if the timer is still running

play46:24

then clear timeout

play46:26

and i'm going to provide the timer id

play46:29

and after this i'm going to set that

play46:31

timer if there's nothing inside the

play46:33

timer then i'm going to say set timeout

play46:38

and this set timeout will have the d

play46:40

that we provided the delay

play46:42

and start the set timeout we're gonna

play46:44

run that function so call back with the

play46:46

arguments args

play46:49

just like this so let me explain this

play46:50

once again so let's say if we are typing

play46:52

something inside of this input box and

play46:54

it came it comes over here so a timer is

play46:56

initialized right so over here

play46:59

it's gonna check if the timer is

play47:01

initialized or not so it was not

play47:02

initialized

play47:03

so

play47:04

it created this set timeout of let's say

play47:06

um let's say one second so it's it has

play47:09

created this set timeout of one second

play47:10

that it's going to only run after one

play47:12

second but one second didn't get

play47:14

completed and user again typed anything

play47:17

inside of the input box then again this

play47:19

will run and it's gonna check if there's

play47:21

something inside the timer then clear it

play47:23

out and this is going to run until user

play47:25

stops the typing and then this set

play47:27

timeout will get to run completely

play47:30

so let's test this out

play47:33

i'm going to create a const

play47:35

handle

play47:37

change

play47:38

and my d bounds oops my d bounce and

play47:43

this will take two things first is going

play47:44

to be a function and other will be the

play47:46

delay so i'm gonna give one second of

play47:48

delay

play47:50

so this d bounce i'm just gonna say

play47:52

console.log and this will take an event

play47:56

event dot target dot value so this comes

play48:00

from here input so i'm gonna say on

play48:02

change and i'm gonna provide handle

play48:04

change

play48:05

yup so this should work let's see let's

play48:08

clear this up

play48:10

and if i'm tapping anything nothing

play48:12

happens but when i stop

play48:14

yes it prints after one second so this

play48:16

is what debounce is all about

play48:18

so yeah i'm typing anything over here

play48:21

yep and we can provide it any time that

play48:23

we like one second two second five

play48:25

second it's your choice so this is how

play48:27

you create a polyfill for rd bounce so

play48:29

yeah that was round three based on

play48:31

react.js and ui stuff and round four was

play48:35

basically an hr plus cultural round this

play48:37

round was more of a discussion on how i

play48:39

would react in different situations and

play48:42

approach problems and team dynamics

play48:44

culture fit etcetera and that concludes

play48:46

all of the rounds for our interview if

play48:48

you like this video give this video a

play48:49

huge fat thumbs up and subscribe to the

play48:51

channel with bell notification turned on

play48:53

and if you would like me to make more

play48:55

such interview videos just let me know

play48:57

in the comment down below and share this

play48:58

video with your friends so that they can

play49:00

also get benefit from these questions

Rate This

5.0 / 5 (0 votes)

関連タグ
Front-endInterview ExperienceJavaScriptReact.jsCoding ChallengesWeb DevelopmentTechnical InterviewCareer AdviceDeveloper InsightsTech Industry
英語で要約が必要ですか?