javascript-interview-freebie
javascript-interview-freebie
md 2024-09-25
const users = [
{ id: 1, name: "Jack", isActive: true },
{ id: 2, name: "John", isActive: true },
{ id: 3, name: "Mike", isActive: false },
];
// Result
// ['Jack', 'John', 'Mike']
let var1;
console.log(var1);
console.log(typeof var1);
Hoisting
Q1: What will be logged here?
console.log(foo);
foo = 1;
console.log(foo);
var foo = 2;
var foo;
foo = 3;
console.log(foo);
Closures
Q1: Create a counter function which has increment and getValue functionality
Q2: Create a function which stores a secret string inside which is not accessible but is returned
only when we call this function.
Currying
Q1: Write a function which helps to achieve multiply(a)(b) and returns multiplication of a and b
console.log(multiply(2)(3)); // 6
Concatenating Arrays
Q2: Write a function which can concatenate 2 arrays
2 / 12
javascript-interview-freebie.md 2024-09-25
const users = [
{ id: 1, name: "Jack", isActive: true },
{ id: 2, name: "John", isActive: true },
{ id: 3, name: "Mike", isActive: false },
];
console.log(isNameExists("Jack", users)); // true
const books = [
{ name: "Harry Potter", author: "Joanne Rowling" },
{ name: "Warcross", author: "Marie Lu" },
{ name: "THe Hunger Games", author: "Suzanne Collins" },
];
console.log(shuffleItems([1, 2]));
3 / 12
javascript-interview-freebie.md 2024-09-25
function getItem() {
console.log(this);
}
getItem();
const item = {
title: "Ball",
getItem() {
console.log(this);
},
};
item.getItem();
class Item {
title = "Ball";
getItem() {
console.log(this);
}
}
const item = new Item();
item.getItem();
class Item {
title = "Ball";
getItem() {
[1, 2, 3].map(function (item) {
console.log(this);
});
}
}
const item = new Item();
item.getItem();
4 / 12
javascript-interview-freebie.md 2024-09-25
Classes
Q1: Design a class for employee which takes id and name in during construction of object and
has a salary property
Q2: Design a class for manager which is employee and can have a department property.
Prototypes
Q1: Design the same classes as in previous question but by using only JavaScript prototypes
and not class keyword.
Iʼve Failed Interview. Whatʼs Next?
Modules
Q1: Create an ES6 module with function getName, getSurname and default export
getFullname.
Q2: Create the same with commonJS module
Q3: What is the differences between ES6 modules and CommonJS modules?
Implement Debounce Function
Q1: Create a debounce function
5 / 12
javascript-interview-freebie.md 2024-09-25
processChange("foo");
}, 1200);
setTimeout(() => {
processChange("foo");
}, 2400);
processChange("foo");
processChange("foo");
<ul class="todo-app">
<li class="item">Walk the dog</li>
<li class="item">Pay bills</li>
<li class="item">Make dinner</li>
<li class="item">Code for one hour</li>
</ul>
Asynchronous Javascript
Xml HTTP Request
Q1: Write an example of fetching data with XMLHttpRequest
Fetch API
Q1: Write an example of fetching data using fetch API
Basic Callback
Q1: Write an asynchronous function which executes callback after finishing it's asynchronous
task
asyncFn((message) => {
console.log("callback", message);
});
Q1: Execute the given list of asynchronous functions in parallel and return the results as an
array to the callback
const users = [
{ id: 1, name: "Jack" },
{ id: 2, name: "John" },
{ id: 3, name: "Mike" },
];
const userStatuses = [
{ id: 1, isActive: true },
{ id: 2, isActive: true },
{ id: 3, isActive: false },
];
const getUsers = () => {
return new Promise((resolve) => {
resolve(users);
});
};
const getUserStatuses = () => {
return new Promise((resolve) => {
resolve(userStatuses);
});
};
7 / 12
javascript-interview-freebie.md 2024-09-25
Comparison Functions
Implement Shallow Comparison
Q1: Design a shallow comparison function
Implement Deep comparison
Q1: Design a deep comparison function
Create Memoization Function
Q1: Design a memorization function which adds 10 to provided value and takes it from cache if
it was already calculated
Tasks Asked Only on Interview
Fibonacci
Q1: Design a function which returns a fibonacci sequence value
Palindrome
Q1: Write a function which checks if string is a palidrome
Anagram
Q1: Write a function which checks if string is an anagram
8 / 12
javascript-interview-freebie.md 2024-09-25
Finding vowels
Q1: Write a function which counts vowels in a string
Convert to Title Case
Q1: Write a function to convert a string to title case
Convert the Time Input Given in 12 Hours Format to 24
Q1: Write a function which can convert the time input given in 12 hours format to 24 hours
format
Mapping Data
Q1: Map data to frontend format. The main element is location key and we need to map all data
to it.
const loc = [
{
location_key: [32, 22, 11],
autoassign: 1,
},
{
location_key: [41, 42],
autoassign: 1,
},
];
const bulkConfig = [
{
dataValues: {
config_key: 100,
},
},
{
dataValues: {
config_key: 200,
},
},
];
// Result
// [
// {
// config_key: 100,
// location_key: 32,
// autoassign: 1
// },
// {
// config_key: 100,
// location_key: 22,
// autoassign: 1
// },
9 / 12
javascript-interview-freebie.md 2024-09-25
// ....
// ]
Validation Messages
Q1: Format backend validation message to frontend format
const backendErrors = {
email: {
errors: [{ message: "Can't be blank" }],
},
password: {
errors: [
{ message: "Must contain symbols in different case" },
{ message: "Must be at least 8 symbols length" },
],
},
passwordConfirmation: {
errors: [{ message: "Must match with password" }],
},
};
// Result
// [
// "Email: Can't be blank",
// "Password: Must contain symbols in different case, Must be
at least 8 symbols length",
// "PasswordConfirmation: Must match with password"
// ]
Nested List
Q1: Transform flat list to nested list
const flatList = [
{
id: 1,
name: "lvl 1 item 1",
parentId: null,
10 / 12
javascript-interview-freebie.md 2024-09-25
},
{
id: 2,
name: "lvl 1 item 2",
parentId: null,
},
{
id: 3,
name: "lvl 2 item 3",
parentId: 1,
},
{
id: 4,
name: "lvl 3 item 4",
parentId: 3,
},
{
id: 5,
name: "lvl 2 item 5",
parentId: 2,
},
];
// Result
// [
// {
// id: 1,
// children: [
// {
// id: 3,
// children: [
// {id: 4, children: []}
// ]
// }
// ]
// },
// {
// id: 2,
// children: [
// {
// id: 5,
// children: []
// }
// ]
// }
// ]
Need help?
All these questions and knowledge might be overwhelming for you. But these are real questions that you
can get on the interview. Luckily, I covered all these questions in my Javascript Interview Questions course
where I explain to you answers to all these questions for different levels of knowledge.
11 / 12
javascript-interview-freebie.md 2024-09-25
12 / 12