avatarKirti K

Summary

The website content provides a comprehensive guide for JavaScript interview preparation, focusing on string manipulation questions and practical examples for beginners.

Abstract

The content is part of a "100 Days of JavaScript Interview Prep" series, specifically detailing Day 7, which covers common string manipulation interview questions. It introduces an online JavaScript playground for running examples, discusses methods to check for empty strings, remove whitespace, count vowels, truncate strings, and hints at upcoming topics such as array manipulation and JavaScript data types. The article encourages interactive learning and coding practice, inviting readers to engage with the material and contribute their solutions to coding challenges.

Opinions

  • The author emphasizes the importance of understanding built-in methods for string manipulation.
  • Interactive coding is encouraged through the use of an online JavaScript playground, suggesting a hands-on approach to learning.
  • The article implies that trimming whitespace and handling strings are fundamental skills for JavaScript developers.
  • Regular expressions are presented as a powerful tool for string manipulation.
  • The author values community engagement and knowledge sharing, as seen in the invitation for readers to comment and contribute.
  • The content is designed to be accessible to beginners, with clear examples and explanations.
  • There is an underlying assumption that readers are preparing for job interviews, indicating the practical nature of the content.
  • The author, Kirti Kaushal, encourages kindness and respect within the coding community, highlighting the importance of a positive learning environment.

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

DAY 7 (March 29, 2024) — Continuing Most Common String Manipulation Interview Questions — Kirri Kaushal

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.

  1. 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)
How can we check if a string is empty or not?

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(''));

Write a function to remove all whitespace characters from a string ?

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);
Write a function to count the number of vowels in a given string /sentence ?

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)
Write a function to count the number of vowels in a given string /sentence ?

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

Other Articles :

Coding Interviews
Javascript Interview
Codingbootcamp
Javascript Development
Programming
Recommended from ReadMedium