{"id":136246,"date":"2024-01-23T17:15:43","date_gmt":"2024-01-23T17:15:43","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?p=136246"},"modified":"2024-01-28T12:55:59","modified_gmt":"2024-01-28T12:55:59","slug":"javascript-code-snippets-for-interview-with-answer","status":"publish","type":"post","link":"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/javascript-code-snippets-for-interview-with-answer\/","title":{"rendered":"Javascript code snippets for interview with Answer"},"content":{"rendered":"\n<p>Certainly! Here are a few JavaScript code snippets that are commonly used in interviews, along with their explanations:<\/p>\n\n\n\n<p><strong>Reverse a String:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to reverse a string.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function reverseString(str) { return str.split('').reverse().join(''); }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Check for Palindrome:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to check if a given string is a palindrome.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function isPalindrome(str) { const reversedStr = str.split('').reverse().join(''); return str === reversedStr; }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Find the Factorial:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to calculate the factorial of a number.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function factorial(n) { if (n === 0 || n === 1) { return 1; } else { return n * factorial(n - 1); } }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>FizzBuzz:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a program that prints the numbers from 1 to 100. But for multiples of 3, print &#8220;Fizz&#8221; instead of the number, and for the multiples of 5, print &#8220;Buzz&#8221;. For numbers which are multiples of both three and five, print &#8220;FizzBuzz&#8221;.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript for (let i = 1; i &lt;= 100; i++) { if (i % 3 === 0 &amp;&amp; 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); } }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Find the Largest Number in an Array:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to find the largest number in an array.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function findLargestNumber(arr) { return Math.max(...arr); }<\/code><\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<p><strong>Check for Prime Number:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to check if a given number is a prime number.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function isPrime(num) { if (num &lt;= 1) return false; for (let i = 2; i &lt;= Math.sqrt(num); i++) { if (num % i === 0) { return false; } } return true; }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Remove Duplicates from an Array:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to remove duplicates from an array.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function removeDuplicates(arr) { return Array.from(new Set(arr)); }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Calculate the Fibonacci Sequence:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to generate the Fibonacci sequence up to a given number of terms.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function fibonacci(n) { const sequence = [0, 1]; for (let i = 2; i &lt; n; i++) { sequence.push(sequence[i - 1] + sequence[i - 2]); } return sequence; }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Check for Anagrams:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Question:<\/strong><br>Write a function to check if two strings are anagrams.<\/li>\n\n\n\n<li><strong>Answer:<\/strong><br><code>javascript function areAnagrams(str1, str2) { const sortedStr1 = str1.split('').sort().join(''); const sortedStr2 = str2.split('').sort().join(''); return sortedStr1 === sortedStr2; }<\/code><\/li>\n<\/ul>\n\n\n\n<p><strong>Find the Nth Element of the Fibonacci Sequence:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to find the nth element of the Fibonacci sequence.<\/li><li><strong>Answer:<\/strong><\/li><\/ul><code>function fibonacciNth(n) { if (n &lt;= 1) return n; let a = 0, b = 1; for (let i = 2; i &lt;= n; i++) { const temp = a + b; a = b; b = temp; } return b; }<\/code><\/p>\n\n\n\n<p><strong>Check for Object Property:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to check if a given object contains a specific property.<\/li><li><strong>Answer:<\/strong><\/li><\/ul><code>function hasProperty(obj, prop) { return prop in obj; }<\/code><\/p>\n\n\n\n<p><strong>Flatten an Array:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to flatten a nested array.<\/li><li><strong>Answer:<\/strong><\/li><\/ul><code>function flattenArray(arr) { return arr.reduce((flat, current) =&gt; flat.concat(Array.isArray(current) ? flattenArray(current) : current), []); }<\/code><\/p>\n\n\n\n<p><strong>Count Occurrences in an Array:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to count the occurrences of each element in an array.<\/li><li><strong>Answer:<\/strong><\/li><\/ul><code>function countOccurrences(arr) { return arr.reduce((acc, val) =&gt; { acc[val] = (acc[val] || 0) + 1; return acc; }, {}); }<\/code><\/p>\n\n\n\n<p><strong>Rotate Array Elements:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to rotate the elements of an array to the right by a specified number of positions.<\/li><li><strong>Answer:<\/strong><\/li><\/ul><code>function rotateArray(arr, positions) { const n = arr.length; const rotated = arr.slice(n - positions).concat(arr.slice(0, n - positions)); return rotated; }<\/code><\/p>\n\n\n\n<p><strong>Deep Clone an Object:<\/strong><ul><li><strong>Question:<\/strong><br>Write a function to create a deep clone of an object.<strong>Answer:<\/strong><\/li><\/ul><code>function deepClone(obj) { return JSON.parse(JSON.stringify(obj)); }<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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(&#8221;).reverse().join(&#8221;); } Check for Palindrome: Question:Write a function to check if a given string is a palindrome. Answer:javascript function isPalindrome(str) { const &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[323],"tags":[],"class_list":["post-136246","post","type-post","status-publish","format-standard","","category-javascript"],"jetpack_publicize_connections":[],"acf":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p99pkJ-zrw","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136246"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=136246"}],"version-history":[{"count":1,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136246\/revisions"}],"predecessor-version":[{"id":136247,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/posts\/136246\/revisions\/136247"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=136246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/categories?post=136246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/tags?post=136246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}