0% found this document useful (0 votes)
12 views2 pages

JavaScript String Interview Q&A

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

JavaScript String Interview Q&A

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JavaScript String Interview Questions & Solutions

1. Reverse a String
function reverseString(str) { return [Link]("").reverse().join(""); }
[Link](reverseString("hello")); // "olleh"

2. Check Palindrome
function isPalindrome(str) { const reversed = [Link]("").reverse().join("");
return str === reversed; } [Link](isPalindrome("madam")); // true

3. Count Vowels and Consonants


function countVowelsConsonants(str) { const vowels = "aeiouAEIOU"; let vCount = 0,
cCount = 0; for (let ch of str) { if (/[a-zA-Z]/.test(ch)) { [Link](ch) ?
vCount++ : cCount++; } } return { vowels: vCount, consonants: cCount }; }
[Link](countVowelsConsonants("javascript")); // { vowels: 3, consonants: 7 }

4. First Non-Repeated Character


function firstNonRepeated(str) { for (let ch of str) { if ([Link](ch) ===
[Link](ch)) return ch; } return null; }
[Link](firstNonRepeated("swiss")); // "w"

5. Check Anagram
function isAnagram(str1, str2) { return [Link]("").sort().join("") ===
[Link]("").sort().join(""); } [Link](isAnagram("listen", "silent")); //
true

6. Remove Duplicate Characters


function removeDuplicates(str) { return [...new Set(str)].join(""); }
[Link](removeDuplicates("programming")); // "progamin"

7. Longest Word in Sentence


function longestWord(sentence) { let words = [Link](" "); return
[Link]((a, b) => ([Link] > [Link] ? a : b)); } [Link](longestWord("I
love JavaScript programming")); // "programming"

8. Count Character Occurrences


function charCount(str) { let obj = {}; for (let ch of str) { obj[ch] = (obj[ch] ||
0) + 1; } return obj; } [Link](charCount("hello")); // { h: 1, e: 1, l: 2, o: 1
}

9. Only Digits Check


function onlyDigits(str) { return /^\d+$/.test(str); }
[Link](onlyDigits("12345")); // true [Link](onlyDigits("12a45")); // false

10. Capitalize First Letter of Each Word


function capitalizeWords(str) { return [Link](/\b\w/g, ch => [Link]());
} [Link](capitalizeWords("hello world from javascript")); // "Hello World From
Javascript"

You might also like