Photo by Claudio Schwarz on Unsplash

Javascript Interview Preparation — Part 2

Parakh Srivastava

--

Hi everyone, in this series of articles, we have already covered some of the topics or code snippets (Part 1). In this article, we will be going through some more regular and essential concepts of core JS that are asked during interviews. This series will surely make you a better JS developer in one way or another. Please don’t forget to follow if you like this article, I will provide more such articles in coming future. With this let’s get started…

Hoisting

function xyz(){
console.log(name);
var name = 'wisdomBearers';
}

xyz();
// O/P: undefined

Hoisting moves the declaration of variables on the top of the block but their initialization remains at the same place. Thus, we get undefined as the above snippet output.

function abc(){
console.log(name);
console.log(age);
console.log(interest);
const interest = 12;
let age = 20;
var name = 'wisdomBearers';
}

abc();
// undefined then Error(cannot access 'b')

For let and const variables, hosting works a bit differently. Although they are moved to the top of the code, still they will remain in the Temporal Dead Zone of the program.

Polyfill for map()

Array.prototype.mapPolyfill = function(callBackFunc) {
// for result
let res = [];

// looping through each element, as map() does, and calling callBackFunc with arguements that it takes
// this[i] : particular element
// i : particular index
// this : whole array context
for(let i=0; i < this.length; i++){
res.push(callBackFunc(this[i], i, this));
}

// returning modified array
return res;
};

const nums = [1,2,3,4];
console.log(nums.mapPolyfill((num, index, nums) => num + 4));
// O/P: [5,6,7,8]

Polyfill for filter()

Array.prototype.filterPolyfill = function(callBackFunc) {
// for result
let res = [];

// looping through each element, as filter() does, and calling callBackFunc with arguements that it takes
for(let i=0; i < this.length; i++){
// pushing in array only if function returns true
if(callBackFunc(this[i], i, this)){
res.push(this[i])
}
}

// returning modified array
return res;
};

const nums = [1,2,3,4];
console.log(nums.filterPolyfill((num, index, nums) => num < 3));
// O/P: [1,2]

Polyfill for reduce()

Array.prototype.reducePolyfill = function(callBackFunc, initialValue) {
let acc = initialValue;

for(let i=0; i < this.length; i++){
// if there is an initialValue then use that, else take first element of array as initialValue
acc = acc ? callBackFunc(acc, this[i], i, this) : this[i];
}

return acc;
};

const nums = [1,2,3,4];
console.log(nums.reducePolyfill((acc, num, index, nums) => acc + num, 0));
// O/P: 10

Output Based Questions

Question 1: All Sports names in CAPS

// given data
let players = [
{sport: 'cricket', age: 20},
{sport: 'football', marks: 31},
{sport: 'hockey', marks: 19},
{sport: 'chess', marks: 34},
];

console.info(players.map(player => player.sport.toUpperCase()));
// O/P: ['CRICKET', 'FOOTBALL', 'HOCKEY', 'CHESS']

Question 2: Guess the O/P

(function square(x){
return (function(y){
console.log(x);
})(2);
})(1);

// O/P: 1 (CLOSURES concept)

Question 3: Guess the O/P

for(var i=0; i < 5; i++){
setTimeout(function() => {
console.log(i);
}, i * 1000);
};

// O/P: 5 5 5 5 5 (since, 'var' is not block scoped)

for(let i=0; i < 5; i++){
setTimeout(function() => {
console.log(i);
}, i * 1000);
};

// O/P: 0 1 2 3 4 (since, 'let' is block scoped)

Question 4: Flatten the Object (Important)

// given 
let obj = {
user: "John",
gender: "Male",
address: {
city: "delhi",
code: "del",
details: {
colony: "saket"
}
}
};

// Output Needed
// {
// 'user':"John",
// 'gender':"Male",
// 'address.city':"delhi",
// 'address.code':"del",
// 'address.details.colony':"saket"
// }

// Generic Implementation
function flatObject(obj) {
let result = {};

// looping over object
for (const i in obj) {
if (typeof obj[i] === "object" && !Array.isArray(obj[i])) {
const temp = flatObject(obj[i]);

// looping over nested object
for (const j in temp) {
result[i + "." + j] = temp[j];
}
}else {
result[i] = obj[i];
}
}

return result;
}

console.log(flatObject(obj));

Conclusion

This brings us to the end of Part 2 of this article. I hope you have learned/revised some valuable concepts of JS. Part 3 will be available soon, which will be having more interesting concepts and output-based questions. Please don’t forget to leave a CLAP if you liked the article. Your valuable suggestions are always welcome.
Happy Learning!!!

--

--