Common JavaScript Interview Coding Challenge

azhar bin Shakil
3 min readNov 1, 2021

1 . Make a function that takes two strings as an argument and determine these are Anagram or not?

Anagram: If two strings are made of the same character mix and the same count of character mix then these will be considered anagrams. Example anagram: friend, finder.

Logic: We can sort both input strings in the same order ASEC/DSEC then we can compare if they are equal then they are anagram . And we have to make them in the same case to ignore case sensitivity.

Program :

const anagram = (str1, str2) => {  str1 = str1.toLowerCase().split("").sort().join("");
str2 = str2.toLowerCase().split("").sort().join(""); return str1 === str2;
}console.log(anagram("Finder", 'friend')); //true

2. FizzBuzz

The FizzBuzz challenge goes something like this:

  • 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

Logic: We can use the modulus operator to know which numbers are multiples with 3, 5, or 3 and 5 both. Like, if a number is multiples of 3, then obviously if we divide the number by 3 there will be no reminder(0). So we can check for the reminder is 0 to determine the number is multiple of any number or not, by this logic we can use a ladder of if else, else if condition to work on.

Program :

const fizzBuzz = (n) => {  for(let i=1; i <= n; i++) {    if(i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz")
}else if(i%3 === 0) {
console.log("Fizz")
}else if(i%5 === 0) {
console.log("Buzz")
}else{
console.log(i);
} }}fizzBuzz(15)---- Output ---1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz

3. Count vowel from a String

Logic: First of all we will write a variable that will contain a count of vowels, we will make it initialize it with 0. The main think , We can make a string of all vowels like ‘aeiou’ then we an use includes method with each character as argument to check the string (string of vowels) contain the character or not, if it’s true then we can mark the character as a vowel and we will increment our count variable

Program :

const vowel =  (str) => {  count = 0;  for(char of str) {    if('aeiou'.includes(char)) {
count++;
}
}
return count;
}console.log(vowel('hello world')) //3

4. Reverse every word of a string.

You have to reverse only every word of a string .

Logic : Hence there is no builtin reverse method for string, but we can split our string into a array with all characters as each item, then we will use the reverse method of array to reverse items , it will make our last character item first character item, then we will join these to convert back to a string until now we will get reversed form of our whole string , now our task is to reverse positions of that word, for that we can split by word with white space separator, we will get a array of words then we will reverse them and join them into a string again with the same separator

--

--