Tricky JavaScript Interview Questions and Answers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

For the first question 2 + '2' does it always concat regardless of the other value? Like what if I do true + '2'?

👍︎︎ 1 👤︎︎ u/ramabhai 📅︎︎ Feb 13 2018 🗫︎ replies

Thanks enjoyed your video.

👍︎︎ 1 👤︎︎ u/[deleted] 📅︎︎ Feb 13 2018 🗫︎ replies
Captions
hey guys and girls not too long ago I released a video on tricky JA scavenger questions and you guys loved it and wanted some more so here is a part 2 when I say tricky means a question answer is never what you expect it to me I just to mess with your mind and when you get such questions you need to read it carefully and understand it before answering the question and welcome to Texas tutorials alright so the first question is let's say if I have two statements like this and one is console logging - number + 2 string and the second is constant logging - number - 2 string what would be the answer of each and why what you can do is pause the video think about it and then tell E my answer with yours - see how you did alright so the answer to this question is for the first one the answer should be 22 and the second one the answer should be 0 and the reason why when I use + operator which means a + operator can be applied to either numbers or strings when I do numb when I have two numbers it would add two numbers when I don't have two numbers what it would do is concat them so we convert the second one into a string and it can get them so that's why I get 22 because of the concatenation and the second one I'm getting 0 because minus sign is not is only a number operator which means it won't it won't work on any string so what it would do is it would convert this string to a number and since it's a number string it would easily convert it into 2 and then 2 minus 2 is 0 that's why I'm getting 0 so yeah we started in a simpler one it's a tricky question but it's not that ik and difficult alright so I have this string of numbers 1 2 2 3 now I want you to remove the duplicates from this array so basically once I remove them I should be getting something like this 1 2 & 3 so that the second 2 is removed now in order to do that I don't want you to use any for loop or any kind of loop I don't want you to use any Map Reduce all those functions I want you to only use a single line to do this and there may be multiple solutions but I'll give you one and if you know any other solution feel free to do that as well so please pause the video think about it and tell you the answer ok so one way to do is is javascript has introduced a new data structure called set so you can do new set and inside if I give a bunch of numbers then it would remove duplicates because set doesn't allow any duplicate values so if I just pass this array inside and console.log this if I run this I would get set which has 1 2 and 3 however I'm getting a set not actually an array so in order to convert into array I can use a spread operator and once I do that it would convert into an array all right so it does so this is how you can do it if you don't know much about spread operator I have a tutorial on utilities on spread operator I provide a link here and also have another video on from my another channel where I teach algorithm data structure and interview questions so there I'm explaining many different ways to do this so I'll provide a link here so you can check it out ok our net quite next question is I have a function and I have a block within that I have two variables Ln V and then L is defined using let and war is defined using Viki of Arc even a V is defined using var keyword and as you know let is a block has block scope and var has a function scope which means if I console.log this thing I it would print V because it's available throughout the function but L doesn't exist outside here it only exists inside this block so if I run this I have it would print war which is this but it would say L is not defined now what I want you to do is I want you to fix this I also don't want war to be available outside so you want to use the VAR keyword to define this variable don't change that and usually what happens when you use a transpiler so basically if you're doing angular react you write your code in es6 where you use let but that gets converted into var if your target is es 5 or so and that's what happens you know you your your war your let wood gets converted to var but you need to do something to fix it because then then your war has function scope and not block scope so you need to do something to make it a block scope so how can you fix this so the answer is using immediately invoked function expression and so if I do this if I use if I wrap the entire thing into another function it's quite ugly and then so my block is replaced by this but I still need to have this block so now if I run this it would say V is not defining what not going further than this because L is also not defined but you would give me a error here so this is how you do it if you don't know what immediately invoked function expression is I I'll provide a link here I have a tutorial on it alright so in our next question is five less six lesson seven what's the answer to this and seven greater than six greater than fine what's the answer today okay so the answer is this should give us true and this should give us fonts even though they're supposed to be true right because seven is greater than 6 square than five and so they both supposed to be true but we are one we are getting true in one is false and that is because let's first try to make sure you will you get it so yes the the first one we're getting true and the second one is false the answer to this question and the answer is true right here is six greater than is seven greater than 6 the answer is true now it's true less than seven now true is what actually when you use a less than operator it will convert into one it will do the same thing here convert into one so is one less than seven yes it is is one greater than five no it isn't so you have false all right so I not next question is I have an error function called a and it basically returns argument arguments and what would be the answer to this so if I console.log it okay so the answer to this is tricky so if I have a function a regular function let's say if I convert this into a regular function so if I say function and because it's a regular function I have to basically return arguments and if I do this it would give me all the arguments and it that I get high here right but if I have an error function then I don't get that hi back and that is because arguments is not does not bind to the error function very well that's why don't use arguments inside an error function you would rather you should rather use something like this okay and return and and now if I run this I would get an array with hi so this is a better way to do it rather than now using an argument okay so the next question I'm sure a lot of you have seen this one but I'm just gonna put it anywhere because it's an interesting one so let's say if I have a function let X equal to and inside I am going to do this return and it's returning some object message if I execute X what would I get all right so let's look at it so if I console.log it I would get undefined and that is because I mean when I was writing it I realize that it's actually giving out by giving you the error so if I have a function return should not have a line break so that's why if I hover over it says light breaking error return so this two are separate becomes separate statement so it's basically returning I think think of like this it's returning nothing actually and that's why you are getting undefined but you really have to if I want to return something I would have to do this okay and then if I run this then I would get high and that is because JavaScript you don't need to enter a semicolon so whenever I have something like this it inserts automatically inserts a semicolon so it does it like this which is bad actually so that's why you're getting this result okay so I had this object profile with some property name so what I want to do is if a user tries to add a new property and say profile dot age equal to three I want user to prevent from from doing this and so if I console.log after this profile it should only have one property so how can I prevent user from adding new more proper new properties so think about it okay so the answer to this question is I would have to do something like this object has a method called freeze and if I use this on profile it would not allow me to add new properties so if I've done this I would only get Texas I don't see their their age okay the second question on this is I want user to modify to be able to modify any property that I already have so if I have a name to equal to Texas I can modify it but I cannot change the age here let's say if I try to do something like this I'd say dot name I'm going to try to change this value to interview next by the way this is my second channel if you're not check it out it's focused on the interviews I'll provide a link here so if I did this it won't allow me to change even this I'm not adding a new property I'm just changing the the current property so it's not allowing me how do I allow user to change the property but still not allow them to add new properties so think about it all right so the answer the question is instead of using freeze you can use seal Co basically doesn't allow you to add new properties but it allows you to modify the D already and it properly so if I find on this now the new property is set to interview nest profile dot h equal to let's say three years fire on this doesn't add any property also you cannot remove property as well so see this lets you change it property but it doesn't let you remove new properties okay even further question to this let's say if I have something like this age is three now what I want you to do is I should be able to modify name but I should not be able to modify age how can I do this so that only only name can be modified but not aged so the one I want you to be able to modify which is name I'll just leave it here and I will just remove it from here and then I would say object dot define property and it should be defined inside profile and then the name of this property should be age and then we will provide its configuration and so this configuration should be the value which is three writable is false which means in the profile add a new property called age value should be three but it's not writable which means you cannot modify it okay so now if I do this profile dot name equal to interview nest profile dot age equal to let's say change it to four and now if I run this as you can see it changed the name to intravenous but the age is still three it did not take this so this is how you can you know do whatever you want all right last but not least what is the answer to this Matt dot max just think about it and I'll will come over the answer all right so the answer to this question is - in finite why why is that so let's just think about it max is a math function so the way it works is when I provide bunch of numbers and console.log it so what would happen here it would find the max of three numbers right so if I run this I would get three you know in order to find it you have to compare something to one and say hey now what is my max here so one so picks a lowest possible value which is in math lowest possible value is minus infinity right there is nothing lower than minus infinity so it takes the minus infinity and compares to one then it says oh one is larger now then it compares one to two then it's two is larger and then it compares two to three three is a larger then it comes comes up with the result three right now if I don't provide any value here then what is the biggest minus infinity because that's the only number available in there so that's why it's getting it so if I do it it's giving me minus infinity and I hope you learned something from this tutorial and if you did please like subscribe and provide a nice comment and you can also help a channel by donating on patreon I'll provide a link here or you can translate the video from me to your native language the link to the translation should be in the description thank
Info
Channel: techsith
Views: 360,415
Rating: 4.927587 out of 5
Keywords: javaScript Interview Questions, Programming Interview, Cracking Coding Interview, Interview questions, techsih, techsith.com
Id: qsNxdukPc2U
Channel Id: undefined
Length: 16min 35sec (995 seconds)
Published: Mon Feb 12 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.