Home / javascript / Javascript code snippets for interview with Answer

Javascript code snippets for interview with Answer

Certainly! Here are a few JavaScript code snippets that are commonly used in interviews, along with their explanations:

Reverse a String:

  • Question:
    Write a function to reverse a string.
  • Answer:
    javascript function reverseString(str) { return str.split('').reverse().join(''); }

Check for Palindrome:

  • Question:
    Write a function to check if a given string is a palindrome.
  • Answer:
    javascript function isPalindrome(str) { const reversedStr = str.split('').reverse().join(''); return str === reversedStr; }

Find the Factorial:

  • Question:
    Write a function to calculate the factorial of a number.
  • Answer:
    javascript function factorial(n) { if (n === 0 || n === 1) { return 1; } else { return n * factorial(n - 1); } }

FizzBuzz:

  • Question:
    Write a program that prints the numbers from 1 to 100. But for multiples of 3, print “Fizz” instead of the number, and for the multiples of 5, print “Buzz”. For numbers which are multiples of both three and five, print “FizzBuzz”.
  • Answer:
    javascript for (let i = 1; i <= 100; 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); } }

Find the Largest Number in an Array:

  • Question:
    Write a function to find the largest number in an array.
  • Answer:
    javascript function findLargestNumber(arr) { return Math.max(...arr); }

Check for Prime Number:

  • Question:
    Write a function to check if a given number is a prime number.
  • Answer:
    javascript function isPrime(num) { if (num <= 1) return false; for (let i = 2; i <= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return true; }

Remove Duplicates from an Array:

  • Question:
    Write a function to remove duplicates from an array.
  • Answer:
    javascript function removeDuplicates(arr) { return Array.from(new Set(arr)); }

Calculate the Fibonacci Sequence:

  • Question:
    Write a function to generate the Fibonacci sequence up to a given number of terms.
  • Answer:
    javascript function fibonacci(n) { const sequence = [0, 1]; for (let i = 2; i < n; i++) { sequence.push(sequence[i - 1] + sequence[i - 2]); } return sequence; }

Check for Anagrams:

  • Question:
    Write a function to check if two strings are anagrams.
  • Answer:
    javascript function areAnagrams(str1, str2) { const sortedStr1 = str1.split('').sort().join(''); const sortedStr2 = str2.split('').sort().join(''); return sortedStr1 === sortedStr2; }

Find the Nth Element of the Fibonacci Sequence:

  • Question:
    Write a function to find the nth element of the Fibonacci sequence.
  • Answer:
function fibonacciNth(n) { if (n <= 1) return n; let a = 0, b = 1; for (let i = 2; i <= n; i++) { const temp = a + b; a = b; b = temp; } return b; }

Check for Object Property:

  • Question:
    Write a function to check if a given object contains a specific property.
  • Answer:
function hasProperty(obj, prop) { return prop in obj; }

Flatten an Array:

  • Question:
    Write a function to flatten a nested array.
  • Answer:
function flattenArray(arr) { return arr.reduce((flat, current) => flat.concat(Array.isArray(current) ? flattenArray(current) : current), []); }

Count Occurrences in an Array:

  • Question:
    Write a function to count the occurrences of each element in an array.
  • Answer:
function countOccurrences(arr) { return arr.reduce((acc, val) => { acc[val] = (acc[val] || 0) + 1; return acc; }, {}); }

Rotate Array Elements:

  • Question:
    Write a function to rotate the elements of an array to the right by a specified number of positions.
  • Answer:
function rotateArray(arr, positions) { const n = arr.length; const rotated = arr.slice(n - positions).concat(arr.slice(0, n - positions)); return rotated; }

Deep Clone an Object:

  • Question:
    Write a function to create a deep clone of an object.Answer:
function deepClone(obj) { return JSON.parse(JSON.stringify(obj)); }

About admin

Check Also

What is hoisting in javascript with example?

Hoisting in JavaScript is a behavior where variable and function declarations are moved to the …

Leave a Reply

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