Ten Common JavaScript Coding Interview Challenges

Hridoy Banik
4 min readMay 16, 2020

1)Palindrome

In palindrome challenge, A string is given to you.You have to show whether that string is palindrome or not.A palindrome is a word, sentence or other type of character sequence which reads the same backward as forward. For “Radar” are palindromes. “people” is not palindromes, because they don’t read the same from left to right and from right to left.

2)The FizzBuzz Challenge

The FizzBuzz challenge goes something like this. Write a function that does the following:

  • console logs the numbers from 1 to n, where n is the integer the function takes as its parameter
  • logs fizz instead of the number for multiples of 3
  • logs buzz instead of the number for multiples of 5
  • logs fizzbuzz for numbers that are multiples of both 3 and 5.

3)Provide some examples of non-bulean value coercion to a boolean one

The question is when a non-boolean value is coerced to a boolean, does it become true or false, respectively?

The specific list of “falsy” values in JavaScript is as follows:

  • "" (empty string)
  • 0, -0, NaN (invalid number)
  • null, undefined
  • false

Any value that’s not on this “falsy” list is “truthy.” Here are some examples of those:

  • "hello"
  • 42
  • true
  • [ ], [ 1, "2", 3 ] (arrays)
  • { }, { a: 42 } (objects)
  • function foo() { .. } (functions)

4)Anagram

A word is an anagram of another word if both use the same letters in the same quantity, but arranged differently.

5)How would you create a private variable in JavaScript?

function func() {
var priv = "secret code";
}

console.log(priv);

To access the variable, a helper function would need to be created that returns the private variable.

function func() {
var pvt= "secret code";
return function() {
return priv;
}
}

var getPvt = func();
console.log(getPvt());

6)Find the Vowels

write a function that takes a string as argument and returns the number of vowels contained in that string.The vowels are “a”, “e”, “i”, “o”, “u”.

7)Fibonacci

A Fibonacci sequence is an ordering of numbers where each number is the sum of the preceding two. For example, the first ten numbers of the Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

8)Write a function that would allow you to do this multiply(5)(6);

function multiply(a) {
return function(b) {
return a * b;
}
}

multiply(5)(6);

9)Explain what a callback function is and provide a simple example.

A callback function is a function that is passed to another function as an argument and is executed after some operation has 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);
});

10)When would you use the “bind” function?

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.

function fullName() {
return "Hello, this is " + this.first + " " + this.last;
}

console.log(fullName()); // => Hello this is undefined undefined

// create a person object and pass its values to the fullName function
var person = {first: "Foo", last: "Bar"};
console.log(fullName.bind(person)()); // => Hello this is Foo Bar

Thank you for reading….

--

--