Javascript Interview Questions For Freshers

10 JavaScript coding interview questions along with their answers and explanations:

Javascript Interview Questions For Freshers

Reverse a String Question:

  1. Write a function that takes a string as input and returns the string reversed.
    Answer:
   function reverseString(str) {
       return str.split('').reverse().join('');
   }

Explanation:
The function splits the string into an array of characters, reverses the array, and then joins it back into a string.

Find the Largest Number in an Array Question:

  1. Write a function that takes an array of numbers as input and returns the largest number in the array.
    Answer:
   function findLargestNumber(arr) {
       return Math.max(...arr);
   }

Explanation:
The spread operator (...) is used to spread the array elements as arguments to Math.max() function, which returns the largest of the given numbers.

Check for Palindrome Question:

  1. Write a function that takes a string as input and returns true if it’s a palindrome, false otherwise. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.
    Answer:
   function isPalindrome(str) {
       const reversedStr = str.split('').reverse().join('');
       return str === reversedStr;
   }

Explanation:
The function first reverses the input string and then compares it with the original string to determine if it’s a palindrome.

FizzBuzz Question:

  1. Write a function that prints numbers from 1 to n, but for multiples of 3, print “Fizz” instead of the number, and for multiples of 5, print “Buzz”. For numbers which are multiples of both three and five, print “FizzBuzz”.
    Answer:
   function 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);
           }
       }
   }

Explanation:
The function iterates through numbers from 1 to n and checks for multiples of 3, 5, or both, printing the appropriate output.

Factorial of a Number Question:

  1. Write a function that calculates the factorial of a given number.
    Answer:
   function factorial(num) {
       if (num === 0 || num === 1) {
           return 1;
       }
       return num * factorial(num - 1);
   }

Explanation:
The function recursively calculates the factorial of the given number by multiplying it with the factorial of the previous number until it reaches 1.

Find the Missing Number in an Array Question:

  1. Write a function that takes an array of numbers from 1 to n, with one number missing, and returns the missing number.
    Answer:
   function findMissingNumber(arr) {
       const n = arr.length + 1;
       const totalSum = (n * (n + 1)) / 2;
       const arrSum = arr.reduce((acc, curr) => acc + curr, 0);
       return totalSum - arrSum;
   }

Explanation:
The function calculates the sum of all numbers from 1 to n using the formula for the sum of an arithmetic series, subtracts the sum of the given array from it, and returns the missing number.

Remove Duplicates from an Array Question:

  1. Write a function that takes an array of numbers and returns a new array with duplicates removed.
    Answer:
   function removeDuplicates(arr) {
       return [...new Set(arr)];
   }

Explanation:
The Set object is used to store unique values, so by converting the array to a set and then back to an array, duplicates are automatically removed.

Check for Prime Number Question:

  1. Write a function that takes a number as input and returns true if it’s a prime number, false otherwise.
    Answer:
   function isPrime(num) {
       if (num <= 1) return false;
       if (num <= 3) return true;
       if (num % 2 === 0 || num % 3 === 0) return false;
       let i = 5;
       while (i * i <= num) {
           if (num % i === 0 || num % (i + 2) === 0) return false;
           i += 6;
       }
       return true;
   }

Explanation:
The function checks if the number is less than or equal to 1, divisible by 2 or 3, or divisible by any number of the form 6k ± 1 up to the square root of the number.

Count Vowels in a String Question:

  1. Write a function that takes a string as input and returns the count of vowels in the string.
    Answer:
   function countVowels(str) {
       const vowels = 'aeiouAEIOU';
       let count = 0;
       for (let char of str) {
           if (vowels.includes(char)) {
               count++;
           }
       }
       return count;
   }

Explanation:
The function iterates through each character of the string and increments a counter whenever it encounters a vowel.

Find the Nth Fibonacci Number Question:

  1. Write a function that takes a number n as input and returns the nth Fibonacci number.

    Answer: function fibonacci(n) { if (n <= 1) return n; let prev = 0, curr = 1; for (let i = 2; i <= n; i++) { let temp = curr; curr += prev; prev = temp; } return curr; } Explanation:
    The function calculates Fibonacci numbers iteratively using a loop, starting from 0 and 1, and updating the values until it reaches the desired nth Fibonacci number.

Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers | Javascript Interview Questions For Freshers

Leave a Comment

Your email address will not be published. Required fields are marked *

Exit mobile version