dayonehk.com

Mastering JavaScript Basics for Interview Success: Day 7

Written on

Chapter 1: Introduction to Day 7

Welcome to Day 7 of our journey through 100 Days of JavaScript Interview Preparation. Today, we’ll dive deeper into the most frequently asked string manipulation questions in interviews. This series aims to enhance your skills through consistent practice and learning.

JavaScript Code Example JavaScript String Manipulation

DAY 7 (March 29, 2024) — Continuing with Common String Manipulation Questions

For those who want to practice coding while reading, we recommend using the online JavaScript Playground linked below. Enjoy coding!

Online JavaScript Playground:

Try experimenting with your code using this interactive platform.

Section 1.1: Checking if a String is Empty

Question 1: How can we determine if a string is empty?

Answer: It's important to understand the methods we use, such as the .trim() method, which eliminates whitespace from both ends of a string and returns a new string without altering the original.

function isEmptyString(str) {

return str.trim().length === 0;

}

console.log(isEmptyString("")); // true

console.log(isEmptyString("Hello")); // false

console.log(isEmptyString(" ")); // true (trimmed length is 0)

JavaScript Empty String Check

Without Trim Output:

function isEmptyString(str) {

return str.length === 0;

}

console.log(isEmptyString("")); // true

console.log(isEmptyString("Hello")); // false

console.log(isEmptyString(" ")); // false (no trimming)

JavaScript String Length Check

Tip: Explore trimEnd() and trimStart() to see how they operate differently.

Section 1.2: Removing Whitespace Characters

Question 2: How do we eliminate all whitespace characters from a string?

Answer:

Method 1 — Using Regular Expressions:

const inputString = 'hello ntHow are you?';

console.log(inputString.replace(/s/g, ''));

JavaScript Remove Whitespace

Method 2 — Using Split and Join:

console.log(inputString.split(/s+/).join(''));

JavaScript Split and Join Example

Section 1.3: Counting Vowels in a String

Question 3: How can we count the number of vowels in a given string?

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) {

if (vowels.includes(character)) {

count++;

}

}

console.log(count);

JavaScript Count Vowels

Method 2 — Using Regular Expressions:

const vowelRegex = /[aeiou]/gi;

const matches = mybestword.match(vowelRegex);

const count = matches ? matches.length : 0;

console.log(count);

JavaScript Count Vowels with Regex

Section 1.4: Truncating a String

Question 4: How can we truncate a string if it's longer than a specified maximum length?

Answer:

const sentence = "my world is my home";

const maxLength = 10;

const truncated = sentence.length > maxLength ? sentence.slice(0, maxLength) + '..' : sentence;

console.log(truncated); // truncated string

JavaScript Truncate String

Section 1.5: Removing Duplicate Words

Question 5: How can we eliminate duplicate words from sentences?

Answer: We invite all our coding friends to share their solutions in the comments!

We will continue our exploration with arrays and JavaScript data types in the upcoming week, from April 1st to April 7th, 2024.

JavaScript Duplicates Example

Stay tuned for more, and happy coding! Remember, kindness is key—share knowledge to foster growth!

If you find this content helpful, consider subscribing and supporting the channel.

Mastering JavaScript - EVERYTHING You Need To Know - YouTube

Explore the essential concepts of JavaScript in this comprehensive guide.

Introduction to Javascript | Complete Course 2024 | Zero to Advanced in 6 hours - YouTube

A complete course designed to take you from beginner to advanced in JavaScript.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Exploring the 'Wave of Death': Insights into Consciousness and Beyond

A deep dive into the phenomenon of the 'wave of death' in consciousness and its implications for understanding life and death.

Breaking Free: Understanding Media Manipulation with Hope and Action

Explore the realities of media manipulation and learn how to navigate it with critical thinking and hope for a more truthful world.

Meditation Advantages: Five Compelling Reasons to Begin Practicing

Discover five compelling reasons to embrace meditation and enhance your mental clarity, decision-making, and overall well-being.