JavaScript Day 6 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey


Welcome to Day 6–100 Days of JavaScript Interview Prep: A Beginner’s Journey
Join us as we document a 100-day adventure filled with learning, practice, and growth
DAY 6 (March 28, 2024) —Continuing Most Common String Manipulation Interview Questions
Note if someone wants to try online while reading :)
Can explore below online javascript Playground — Happy Coding!
Online New JavaScript Playground with live changes
We will use online JavaScript Compiler today to run all examples of questions
Question 1 — Write a function to convert a given string to title case?
Tips : ask all permutation and combination of the requirements
Considering here —
a) Converting to uppercase the first letter of every word in a sentence, and the rest of other ones remain in lowercase.
Refer Day 5 for built-in method description and usage with examples in detail
Answer :
let str = 'my pEt nAme is cuFFU'
str.toLowerCase();
console.log(str.toLowerCase()) // all in lowercase
// split
str.toLowerCase().split(' ')
console.log(str.toLowerCase().split(' ')) // array split
// now add loop and substring
const newstr=str.toLowerCase().split(' ').map((w=>{
return (w.charAt(0).toUpperCase() + w.substring(1))
})).join(' ')
// slice
const newstr1=str.toLowerCase().split(' ').map((w=>{
return (w.charAt(0).toUpperCase() + w.slice(1))
})).join(' ')
// answer
console.log(newstr)
console.log(newstr1)

Tip : Now find the difference between substring and Slice — Reader’s contributions
Provide comments which is best to use and why :). Happy coding
Other Methods -
We can also use reduce, for loop methods please try and provide solutions in comments.
We will add all solutions here as contributors and make our page more engaging :)
Question 2 — Check if a String is Palindrome or Not ?
Tip — Always ask the requirement if hearing first-time, some words.
Ask for Clarification: Politely ask the interviewer to clarify the question if you don’t understand it fully.
Answer :
What is Palindrome ?
A palindrome is a word, number, phrase, or other sequence of symbols that reads the same backwards as forwards.
For example : radar, madam or racecar etc
let mystr = 'radar' // Step 1
mystr.split('') // convert to array // Step 2
mystr.split('').reverse() // reverse the string // Step 3
mystr.split('').join() // join the array
console.log(mystr.split('').reverse().join(' '))
// final code

Final Code
function isPalindrome(str){
const reserved = str.split('').reverse().join('');
return reserved === str
}
const mystr = 'radar'
console.log(isPalindrome(str)); // true
const mystr = 'radar1'
console.log(isPalindrome(str)); // false
Question 3 — Write a code to check if two given strings are anagrams of each other ?
Answer:
What is anagram ?
A word or phrase, the letters of which can be rearranged into another word or phrase.
Anagrams: bare/bear, reed/deer, calm/clam, ales/seal, toga/goat, hear/hare, aunt/tuna, paws/wasp, flow/wolf
let str1= 'silent'
let str2='listen'
// Step 1 str1.split('') // convert to array
// Step 2 - sort array
// Step 3 - join
const word1 = str1.split('').sort().join('');
const word2 = str2.split('').sort().join('');
console.log(word1 ,'',word2)
// output
eilnst
eilnst
Final Code
function isAnagram(str1, str2) {
const sortStr1 = str1.split('').sort().join('');
const sortStr2 = str2.split('').sort().join('');
return sortStr1 === sortStr2;
}
console.log(isAnagram('radar', 'darar'));
// Output: true
Question 4 — Write a function to check if a given substring exists within a larger sentence.
Answer :
Before diving into the solution, it’s essential to understand why we utilize certain built-in methods.
includes() Method
The includes() method of String values performs a case-sensitive search to determine whether a given string may be found within this string, returning true or false as appropriate.
const sentence = 'i love painting.';
const word = 'painting';
str.includes(word)
console.log(
`The word "${painting}" ${
sentence.includes(word) ? 'is' : 'is not'
} in the sentence`,
);
// The word "painting" is in the sentence
Final Code
function hasWordInSentence(sentence, word) {
return sentence.includes(word);
}
console.log(hasWordInSentence('hello friend', 'friend')); // Output: true
Question 5 — Write a function to replace all occurrences of a substring/word within a string/sentence with another substring/word.
Answer :
Before diving into the solution, it’s essential to understand why we utilize certain built-in methods.
Split()
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
const myString = "this is my home";
const resultArr = myString.split('home'); // home with -
console.log(resultArr.join("-")); // home replaced with -
final output // this is my -
Step 1 Word/Substring by first passing in the substring to be replaced in the split() method
Step 2 using the join() method to join the returned array with the new substring/word.
Note — The split() method searches for the passed-in separator argument in the string.
Final Code
function replaceWord(sentence, oldWord, newWord) {
return sentence.split(oldWord).join(newWord);
}
console.log(replaceWord('hello my name is tom', 'tom', 'cat'));
//hello my name is cat
String manipulation is indeed a vast topic with huge possibilities.
We can continue exploring more examples over the next few days.
Happy learning :)
Kind is cool !!! Share knowledge to gain knowledge !!!! Be kind and treat people with respect !!
If you find my videos helpful, then please subscribe, Claps and follow to support
Cheers, Kirti Kaushal







