JavaScript Basic Interview Questions for Mastering String
Q1:- Reverse a String Question: Write a function to reverse a string. Ans function reverseString(str) { return str.split('').reverse().join(''); } console.log(reverseString('hii')); // Output: "iih" Code Q2:- Check for Palindrome Question: Determine if a given string is a palindrome. Ans function Check_Palindrome(str){ let reversed = str.split('').reverse().join(''); if(str === reversed){ console.log(`${str} is a Palindreme Number`) }else{ console.log(`${str} is not a Palindreme Number`) } } console.log(Check_Palindrome('11a')) Code Q3:- How do you remove whitespace from both ends of a string in JavaScript? Code Q4:- How do you extract a section of a string in JavaScript? You can use the slice() method in JavaScript to extract a section of a string and return it as a new string. Example: let str = "Hello World!"; let slicedStr = str.slice(0, 5); console.log(slicedStr); // "Hello" Code Q5:- Sort a String in JavaScript You can use the split(), sort(), and join() methods to sort a string in JavaScript. Example: let str = "IHGFEDCBA" let Shorted_String = str.split('').sort().join() console.log(Shorted_String) // A,B,C,D,E,F,G,H,I Code Q6:- Write Separate Functions for Each Social Media Platform to Extract Usernames from the URLs Write separate functions for each of the following social media platforms that will extract the username from the URL. The URL format for each platform is provided below. Each function should take a URL as input and return the corresponding username from that URL. Question: Instagram URL format: https://www.instagram.com/{username}/ Facebook URL format: https://www.facebook.com/{username} LinkedIn URL format: https://www.linkedin.com/in/{username}/ Twitter URL format: https://twitter.com/{username} YouTube URL format: https://www.youtube.com/c/{username} Pinterest URL format: https://www.pinterest.com/{username}/ Snapchat URL format: https://www.snapchat.com/add/{username} TikTok URL format: https://www.tiktok.com/@{username} Reddit URL format: https://www.reddit.com/u/{username}/ GitHub URL format: https://github.com/{username} WhatsApp URL format: https://wa.me/{username} Tumblr URL format: https://{username}.tumblr.com/ Spotify URL format: https://open.spotify.com/user/{username} Skype URL format: https://join.skype.com/{username} Flickr URL format: https://www.flickr.com/photos/{username}/ Exaple function getInstagramUsername(url) { return url.split('instagram.com/')[1].split('/')[0]; } Code

Q1:- Reverse a String
- Question: Write a function to reverse a string.
Ans
function reverseString(str) {
return str.split('').reverse().join('');
}
console.log(reverseString('hii')); // Output: "iih"
Q2:- Check for Palindrome
- Question: Determine if a given string is a palindrome.
Ans
function Check_Palindrome(str){
let reversed = str.split('').reverse().join('');
if(str === reversed){
console.log(`${str} is a Palindreme Number`)
}else{
console.log(`${str} is not a Palindreme Number`)
}
}
console.log(Check_Palindrome('11a'))
Q3:- How do you remove whitespace from both ends of a string in JavaScript?
Q4:- How do you extract a section of a string in JavaScript?
You can use the slice()
method in JavaScript to extract a section of a string and return it as a new string.
Example:
let str = "Hello World!";
let slicedStr = str.slice(0, 5);
console.log(slicedStr); // "Hello"
Q5:- Sort a String in JavaScript
You can use the split()
, sort()
, and join()
methods to sort a string in JavaScript.
Example:
let str = "IHGFEDCBA"
let Shorted_String = str.split('').sort().join()
console.log(Shorted_String) // A,B,C,D,E,F,G,H,I
Q6:- Write Separate Functions for Each Social Media Platform to Extract Usernames from the URLs
Write separate functions for each of the following social media platforms that will extract the username from the URL. The URL format for each platform is provided below. Each function should take a URL as input and return the corresponding username from that URL.
- Question:
-
Instagram
- URL format:
https://www.instagram.com/{username}/
- URL format:
-
Facebook
- URL format:
https://www.facebook.com/{username}
- URL format:
-
LinkedIn
- URL format:
https://www.linkedin.com/in/{username}/
- URL format:
-
Twitter
- URL format:
https://twitter.com/{username}
- URL format:
-
YouTube
- URL format:
https://www.youtube.com/c/{username}
- URL format:
-
Pinterest
- URL format:
https://www.pinterest.com/{username}/
- URL format:
-
Snapchat
- URL format:
https://www.snapchat.com/add/{username}
- URL format:
-
TikTok
- URL format:
https://www.tiktok.com/@{username}
- URL format:
-
Reddit
- URL format:
https://www.reddit.com/u/{username}/
- URL format:
GitHub
- URL format:
https://github.com/{username}
- URL format:
https://wa.me/{username}
- Tumblr
- URL format:
https://{username}.tumblr.com/
- Spotify
- URL format:
https://open.spotify.com/user/{username}
- Skype
- URL format:
https://join.skype.com/{username}
- Flickr
- URL format:
https://www.flickr.com/photos/{username}/
Exaple
function getInstagramUsername(url) {
return url.split('instagram.com/')[1].split('/')[0];
}