10 Javascript Coding Questions and Answers

10 Javascript Coding Questions and Answers

Are you preparing for a technical interview with a top-tier organization, or are you simply looking to improve your JavaScript coding skills? In this post, we’ve gathered a list of ten JavaScript code interview questions and answers to put your problem-solving and coding skills to the test. These questions cover a broad range of JavaScript concepts, including array manipulation and string operations. Whether you’re an experienced developer wishing to refresh your abilities or new to the world of programming, these questions and detailed answers will help you prepare successfully and build your confidence.

Let’s dig into the world of JavaScript logic and prepare to ace your upcoming interview!

Javascript Coding Questions and Answers

1. Write a function to find the factorial of a number using recursion.

Answer:

function factorial(n) {
  if (n === 0) return 1;
  return n * factorial(n - 1);
}
JavaScript

2. Implement a function to merge two sorted arrays into a single sorted array without using any built-in functions.

Answer:

function mergeSortedArrays(arr1, arr2) {
  let result = [];
  let i = 0;
  let j = 0;
  while (i < arr1.length && j < arr2.length) {
    if (arr1[i] < arr2[j]) {
      result.push(arr1[i]);
      i++;
    } else {
      result.push(arr2[j]);
      j++;
    }
  }
  return result.concat(arr1.slice(i), arr2.slice(j));
}
JavaScript

3. Write a function to check if a given string is a palindrome.

Answer:

function isPalindrome(str) {
  str = str.toLowerCase().replace(/[^a-zA-Z0-9]/g, '');
  return str === str.split('').reverse().join('');
}
JavaScript

4. Given a sorted array, implement a function to find the index of a specific element using binary search.

Answer:

function binarySearch(arr, target) {
  let left = 0;
  let right = arr.length - 1;
  while (left <= right) {
    let middle = Math.floor((left + right) / 2);
    if (arr[middle] === target) return middle;
    if (arr[middle] < target) left = middle + 1;
    else right = middle - 1;
  }
  return -1;
}
JavaScript

5. Implement a function to find the most frequently occurring element in an array.

Answer:

function mostFrequentElement(arr) {
  const count = {};
  let mostFrequent;
  let maxCount = 0;
  for (const item of arr) {
    count[item] = (count[item] || 0) + 1;
    if (count[item] > maxCount) {
      mostFrequent = item;
      maxCount = count[item];
    }
  }
  return mostFrequent;
}
JavaScript

6. Implement a function to reverse the order of words in a string.

Answer:

function reverseWords(str) {
  return str.split(' ').reverse().join(' ');
}
JavaScript

7. Given an array of numbers, implement a function to find the second largest number.

Answer:

function secondLargest(arr) {
  arr = [...new Set(arr)]; // Remove duplicates
  if (arr.length < 2) return "Not enough elements";
  arr.sort((a, b) => b - a);
  return arr[1];
}
JavaScript

8. Implement a function to check if a string is an anagram of another string.

Answer:

function areAnagrams(str1, str2) {
  str1 = str1.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
  str2 = str2.replace(/[^\w]/g, '').toLowerCase().split('').sort().join('');
  return str1 === str2;
}
JavaScript

9. Implement a function to find and return the first non-repeated character in a string.

Answer:

function firstNonRepeatedChar(str) {
  const charCount = {};
  for (let char of str) {
    charCount[char] = (charCount[char] || 0) + 1;
  }
  for (let char of str) {
    if (charCount[char] === 1) return char;
  }
  return null;
}
JavaScript

10. Implement a function to rotate a 2D matrix (NxN) 90 degrees clockwise.

Answer:

function rotateMatrix(matrix) {
  const n = matrix.length;
  const rotated = new Array(n).fill().map(() => new Array(n).fill(0));
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      rotated[j][n - 1 - i] = matrix[i][j];
    }
  }
  return rotated;
}
JavaScript

Conclusion

These JavaScript code questions span a wide range of programming ideas and are frequently used in technical interviews to assess problem-solving ability, algorithmic thinking, and coding skills, making them an invaluable tool for interview preparation.

FAQ

Why is JavaScript important for web development?

JavaScript is a flexible programming language that runs in browsers to create dynamic and interactive web pages. It’s necessary for client-side scripting, which improves user experience.

Explain the concept of closures in JavaScript.

Closures enable functions to keep access to variables from their containing scope even after it has completed execution. They allow you to construct private variables and functions.

What is the purpose of the this keyword in JavaScript?

This keyword refers to the current execution context. In a global context, it refers to the global object; in a function, it depends on how it is invoked.

Explain the concept of promises in JavaScript.

Promises are objects that reflect the eventual success or failure of an asynchronous action. They simplify and make asynchronous programs more readable.

What is the difference between == and === in JavaScript?

== applies type coercion, attempting to convert operands to the same type before comparing, whereas === checks both value and type without coercion, resulting in a strict comparison.

Have questions about this blog? Contact us for assistance!