Lesson 2
String Manipulation and Nested Loops
Introduction

Welcome to this engaging lesson on string manipulation! In this lesson, we will dive into the fundamental concept of nested loops and explore them through an intriguing challenge. Our task will involve extracting particular characters from words within a sentence using these loops. Are you ready to embark on this fascinating exploration? Let's get started!

Task Statement

The task we'll undertake is as follows: We will work with a string that represents a sentence where words are separated by spaces. Your challenge is to create a function that identifies the odd-indexed characters of words with an even number of characters, including punctuation marks as part of the word's length, and combines these characters into a single string, preserving the order in which they appeared in the sentence.

Consider this example: "TypeScript programming language is versatile." The word TypeScript has 10 characters (an even number), and we'll select the odd-indexed characters from this word, namely y, e, c, i, t. Similarly, from language, we select a, g, a, e; from is, we select s; and from versatile., including the punctuation, we select e, s, t, l, .. We'll skip the word programming as it has an odd length.

The expected final output for our function should be "yecitagaestl.". Let's delve into how to achieve this!

Solution Building: Step 1

We'll begin by splitting the sentence into words, which will be stored in an array.

TypeScript
1function oddCharsFromEvenWords(sentence: string): string { 2 const words = sentence.split(" "); 3 // we will proceed progressively 4}
Solution Building: Step 2

Now, we introduce nested loops: an outer loop that iterates over each word and an inner loop that checks every character within the word. We'll use an if condition to verify if a word has an even length, determined by checking if the length of the word mod 2 equals zero.

TypeScript
1function oddCharsFromEvenWords(sentence: string): string { 2 const words: string[] = sentence.split(" "); 3 4 for (const w of words) { 5 if (w.length % 2 === 0) { // checks if the length of the word is even 6 // we are building up our solution gradually 7 } 8 } 9}
Solution Building: Step 3

With our outer loop set, let's implement the inner loop. We will iterate only over the odd-indexed characters in words with even length by starting from an index of 1 and incrementing by 2 each time. We'll append these characters to our result string, which will be returned as our final output.

TypeScript
1function oddCharsFromEvenWords(sentence: string): string { 2 const words: string[] = sentence.split(" "); 3 4 let result: string = ""; 5 for (const w of words) { 6 if (w.length % 2 === 0) { // confirms if the length of the word is even 7 for (let i: number = 1; i < w.length; i += 2) { // loops over odd-indexed characters 8 result += w.charAt(i); 9 } 10 } 11 } 12 return result; 13} 14 15console.log(oddCharsFromEvenWords("TypeScript programming language is versatile.")); 16// Output: yecitagaesestl.
Lesson Summary

Congratulations! You've successfully navigated the concept of nested loops to extract specific characters from words in a sentence. This lesson has enhanced your ability to analyze and manipulate strings using TypeScript's robust type system. As you continue your journey, keep practicing with nested loops and string manipulations to deepen your understanding and skill set. The more you apply your knowledge in TypeScript, the more proficient you will become. Let's continue to explore the vast possibilities that TypeScript offers!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.