Home > Web Front-end > JS Tutorial > JavaScript interview questions & answers with code

JavaScript interview questions & answers with code

Susan Sarandon
Release: 2024-12-29 05:59:11
Original
249 people have browsed it

Top JavaScript Interview Questions and Expert Answers with Code Examples

Flatten a Nested Object

Question : Write a function to flatten a nested JavaScript object.

Code :

function flattenObject(obj, prefix = '', res = {}) {

** for (let key in obj) {**

** const newKey = prefix ? ${prefix}.${key} : key;**

** if (typeof obj[key] === 'object' && obj[key] !== null) {**

** flattenObject(obj[key], newKey, res);**

** } else {**

** res[newKey] = obj[key];**

** }**

** }**

** return res;**

}

console.log(flattenObject({ a: { b: { c: 1 } }, d: 2 }));

// Output: { 'a.b.c': 1, d: 2 }
Copy after login

2. Find Duplicates in an Array

Question : Write a function to find duplicate values in an array.

Code :

function findDuplicates(arr) {

** const counts = {};**

** return arr.filter(item => counts[item] ? true : (counts[item] = 1, false));**

}

console.log(findDuplicates([1, 2, 2, 3, 4, 4, 5]));

// Output: [2, 4]

Copy after login

3. Implement debounce
Question : Write a debounce function that limits the rate a function is called.

Code :

function debounce(func, delay) {

** let timer;**

** return function (...args) {**

** clearTimeout(timer);**

** timer = setTimeout(() => func.apply(this, args), delay);**

** };**

}

const log = debounce(() => console.log('Logged after 1s'), 1000);

log();

Copy after login

4. Reverse a String Recursively

Question : Write a recursive function to reverse a string.

Code :

function reverseString(str) {

** if (str === "") return "";**

** return reverseString(str.slice(1)) + str[0];**

}

console.log(reverseString("hello"));

// Output: "olleh"
Copy after login

5. Check for Palindrome

Question : Write a function to check if a string is a palindrome.

Code :

function isPalindrome(str) {

** const cleaned = str.toLowerCase().replace(/[^a-z]/g, '');**

** return cleaned === cleaned.split('').reverse().join('');**

}

console.log(isPalindrome("A man, a plan, a canal, Panama"));

// Output: true
Copy after login

Read more... ⇲

JavaScript interview questions & answers with code

JavaScript interview questions & answers with code

JavaScript interview questions & answers with code

JavaScript interview questions & answers with code

React interviews questions and answers with code

React interviews questions and answers with code

JavaScript interview questions & answers with code

Angular interview questions

Angular interview questions

JavaScript interview questions & answers with code

CSS Interview Questions

CSS Interview Questions along with their answers and Code

JavaScript interview questions & answers with code atharvgyan.com

The above is the detailed content of JavaScript interview questions & answers with code. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template