JavaScript Day 7— Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation
100 Days of JavaScript Interview Prep: A Beginner’s Journey
Welcome to Day 7–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 7 (March 29, 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 : How can we check if a string is empty or not?
Answer : Before diving into the solution, it’s essential to understand why we utilize certain built-in methods.
- trim() Method -values removes whitespace from both ends of this string and returns a new string, without modifying the original string.
function isEmptyString(str) {
return str.trim().length === 0;
}
console.log(isEmptyString("")); // true
console.log(isEmptyString("Hello")); // false
console.log(isEmptyString(" ")); // true (since trimmed length becomes 0)
Without Trim Output
function isEmptyString(str) {
return str.length === 0;
}
console.log(isEmptyString("")); // true
console.log(isEmptyString("Hello")); // false
console.log(isEmptyString(" ")); // false (since trim is removed)
Tip : Other Trim method’s trimEnd and trimStart
Please try below method to identify the difference how both works and what difference of results.
Question 2 : Write a function to remove all whitespace characters from a string ?
const inputString = ‘hello world\n\tHow are you?’
Answer :
Method 1 — Regular Expression
const inputString = 'hello \n\tHow are you?'
// Example 1
console.log(inputString.replace(/\s/g,''));

Method 2 : Using Split and Join()
const inputString = 'hello \n\tHow are you?'
console.log(inputString.split(/\s+/).join(''));

Question 3 : Write a function to count the number of vowels in a given string /sentence ?
const mybestword= “my best world is my home only”
Answer :
Method 1 — Simple For loop
const sentence="my best world is my home only";
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
let count = 0 ;
for(let character of sentence){
// check character is a vowels
if(vowels.includes(character)){
count++;
}
}
console.log(count);
Method 2 Regular Expression
const mybestword= "my best world is my home only"
const vowelRegex = /[aeiou]/gi;
const matches = mybestword.match(vowelRegex);
const count = matches ? matches.length : 0;
console.log(count)
Question 4:Write a code to truncate a string if it’s longer than maximum length ?
Consider maximum length === 10
Answer :
const sentence = "my world is my home";
const maxLength = 10
sentence.length // calculate length first
const tt= sentence.length > maxLength ? sentence.slice(0,maxLength) + '..' : sentence;
console.log(tt); // truncated string
console.log(sentence); // orignal string

Question 5 : How to remove duplicates words from sentences ?
Answer :
Assignment to my all coder friends , please add comments for your answers :)
Assignment for our technical readers to find the answer for our audience
Happy coding :)
We will continue with our next section Arrays and Javascript Data Type in our next week April 1st to April 7th ,2024 ..

Stay Tuned and 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





