Common JavaScript Interview Coding Challenge

Solaiman Shadin
3 min readMay 16, 2020

1 . Make a function that take two string as argument and determine these are Anagram or not ?

Anagram : If two string made of same character mix and same count of character mix then these will be considered as anagram . Example anagram : friend , finder .

Logic : We can sort both input string in same order ASEC/DSEC then we can compare if the are equal then they are anagram . And we have to make them in 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 modulus operator to know which number 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 which will contain count of vowel , 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 character as each item , then we will use the reverse method of array to reverse items , it will make our last character item to first character item, then we will join these to convert back to string , until now we will get reversed form of our whole string , now our task is to reverse positions of those 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 string again with same saperator

--

--