Fullstack Cafe JavaScript Challenges
10 JavaScript Coding Problems from Fullstack Cafe - Milestone 3 (Day 6)
Today we’ve practiced 25 JavaScript coding interview problems from fullstack.cafe. Here we’ve presented 10 of them.
1. Checking Anagrams of one another
Problems:
Given two strings, return true if they are anagrams of one another.
Solution:
What is Anagram?
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, the word anagram can be rearranged into nag a ram, or the word binary into brainy or the word adobe into abode.
2. Callback function
Problem:
Explain what a callback function is and provide a simple example.
Solution:
A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed. Below is an example of a simple callback function that logs to the console after some operations have been completed.
function modifyArray(arr, callback) {
// do something to arr here
arr.push(100);
// then execute the callback function that was passed
callback();
}
var arr = [1, 2, 3, 4, 5];
modifyArray(arr, function() {
console.log("array has been modified", arr);
});The output will be:
3. FizzBuzz Challenge
Problem:
Create a for loop that iterates up to 100 while outputting "fizz" at multiples of 3, "buzz" at multiples of 5 and "fizzbuzz" at multiples of 3 and 5.
Solution:
for (let i = 1; i <= 100; i++) {
let f = i % 3 == 0,
b = i % 5 == 0;
console.log(f ? (b ? 'FizzBuzz' : 'Fizz') : b ? 'Buzz' : i);
}The output will be:
4. “this” keyword
Problem:
How does the “this” keyword work? Provide some code examples.
Solution:
In JavaScript, this always refers to the “owner” of the function we’re executing, or rather, to the object that a function is a method of.
function foo() {
console.log( this.bar );
}
var bar = "global";
var obj1 = {
bar: "obj1",
foo: foo
};
var obj2 = {
bar: "obj2"
};The output will be:
5. Recursive function
Problem:
Write a recursive function that performs a Binary Search
Solution:
function recursiveBinarySearch(array, value, leftPosition, rightPosition) { if (leftPosition > rightPosition){
return -1;
}
var middlePivot = Math.floor((leftPosition + rightPosition) / 2);
if (array[middlePivot] === value) {
return middlePivot;
} else if (array[middlePivot] > value) {
return recursiveBinarySearch(array, value, leftPosition, middlePivot - 1);
} else {
return recursiveBinarySearch(array, value, middlePivot + 1, rightPosition);
}
}
The output will be:
6. Closure
Problem:
What is “closure” in javascript? Provide an example?
Get Rahul Biswas’s stories in your inbox
Join Medium for free to get updates from this writer.
Solution:
What is Closure?
A closure is a function defined inside another function (called parent function) and has access to the variable which is declared and defined in parent function scope.
A closure can access:
i) Variable declared in his own scope
ii) Variable declared in parent function scope
iii) Variable declared in global namespace
var globalVar = "abc";
// Parent self invoking function
(function outerFunction (outerArg) { // begin of scope outerFunction
// Variable declared in outerFunction function scope
var outerFuncVar = 'x';
// Closure self-invoking function
(function innerFunction (innerArg) { // begin of scope innerFunction
// variable declared in innerFunction function scope
var innerFuncVar = "y";
console.log(
"outerArg = " + outerArg + "\n" +
"outerFuncVar = " + outerFuncVar + "\n" +
"innerArg = " + innerArg + "\n" +
"innerFuncVar = " + innerFuncVar + "\n" +
"globalVar = " + globalVar);
// end of scope innerFunction
})(5); // Pass 5 as parameter
// end of scope outerFunction
})(7); // Pass 7 as parameterThe output will be:
7. “delete” operator
Problem:
What will be the output of the following code?
Solution:
The output will be 0. delete operator is used to delete a property from an object. Here x is not an object it's local variable. delete operator doesn't affect local variable.
8. “bind” function
Problem:
When would you use the “bind” function?
Solution:
The bind() method creates a new function that, when called, has its this keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function is called.
A good use of the bind function is when you have a particular function that you want to call with a specific this value. You can then use bind to pass a specific object to a function that uses a this reference.
9. Private Variable
Problem:
How would you create a private variable in JavaScript?
Solution:
To create a private variable in JavaScript that cannot be changed you need to create it as a local variable within a function. Even if the function is executed the variable cannot be accessed outside of the function. For example:
To access the variable, a helper function would need to be created that returns the private variable.
10. Enqueue and Dequeue
Problem:
Implement enqueue and dequeue using only two stacks.
Solution:
Enqueue means to add an element, dequeue to remove an element.
var inputStack = []; // First stack
var outputStack = []; // Second stack
// For enqueue, just push the item into the first stack
function enqueue(stackInput, item) {
return stackInput.push(item);
}
function dequeue(stackInput, stackOutput) {
// Reverse the stack such that the first element of the output stack is the
// last element of the input stack. After that, pop the top of the output to
// get the first element that was ever pushed into the input stack
if (stackOutput.length <= 0) {
while(stackInput.length > 0) {
var elementToOutput = stackInput.pop();
stackOutput.push(elementToOutput);
}
}
return stackOutput.pop();
}The output will be:
That’s all for today…Thanks for reading. Give a clap if you liked this article. Happy Learning :)
Article Reference:
https://www.fullstack.cafe/blog/javascript-code-interview-questions









