avatarKirti K

Summary

The web content outlines JavaScript Day 6 of a 100-day interview preparation journey, focusing on string manipulation interview questions, including converting strings to title case, checking for palindromes, determining if strings are anagrams, and replacing substrings within sentences.

Abstract

The provided web content is part of a comprehensive guide for JavaScript interview preparation, specifically detailing the sixth day of a 100-day learning journey. It delves into common string manipulation questions that are often encountered during interviews. The article begins by introducing an online JavaScript playground for readers to practice coding examples in real-time. It then proceeds to present solutions to five string manipulation challenges: converting a string to title case, checking if a string is a palindrome, determining if two strings are anagrams, checking for the existence of a substring within a sentence, and replacing all occurrences of a substring with another. Each question is accompanied by a detailed explanation, code examples, and encouragement for reader contributions. The content emphasizes the importance of understanding string methods and encourages readers to engage with the material by experimenting with the provided code snippets and discussing alternative solutions in the comments.

Opinions

  • The author values interactive learning, providing an online JavaScript playground for hands-on practice.
  • There is an emphasis on the importance of asking clarifying questions during interviews, as demonstrated with the palindrome and anagram questions.
  • The author encourages readers to explore beyond the provided solutions, suggesting the use of other methods like reduce and for loops for string manipulation.
  • The content promotes a collaborative learning environment by inviting readers to share their insights and alternative solutions in the comments.
  • The author believes in the pedagogical approach of learning through repetition and practice, as evidenced by the 100-day structured learning plan.
  • There is a focus on the practical application of JavaScript basics, aligning with the goal of interview preparation.
  • The author advocates for kindness and respect within the tech community, highlighting the value of sharing knowledge.

JavaScript Day 6 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

DAY 6 (March 28, 2024) — Continuing Most Common String Manipulation Interview Questions

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 
Check if a String is Palindrome or Not ?

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
Check if a String is Palindrome or Not ?

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
check if two given strings are anagrams of each other

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
Check if two given strings are anagrams of each other

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
Write a function to check if a given substring exists within a larger sentence

Final Code

function hasWordInSentence(sentence, word) {
    return sentence.includes(word);
}
console.log(hasWordInSentence('hello friend', 'friend')); // Output: true
Write a function to check if a given substring exists within a larger sentence

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

Other Articles

JavaScript
Coding Interviews
Javascript Interview
Programming
Beginner Coding
Recommended from ReadMedium