Warmup: TypeScript
String Methods
Input (common for all exercises):
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
Exercise 1:
Task: Convert the given string to uppercase using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.toUpperCase();
console.log(result); // Output: 'HELLO, TYPESCRIPT! THIS IS A TEST STRING FOR TYPESCRIPT EXERCISES.'
Exercise 2:
Task: Extract the substring starting from index 7 to 17 from the given string using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.substring(7, 18);
console.log(result); // Output: 'Typescript'
Exercise 3:
Task: Check if the given string starts with the word "Hello" using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.startsWith('Hello');
console.log(result); // Output: true
Exercise 4:
Task: Check if the given string ends with the word "exercises" using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.endsWith('exercises');
console.log(result); // Output: true
Exercise 5:
Task: Count the number of occurrences of the word "Typescript" in the given string using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const occurrences = inputString.split('Typescript').length - 1;
console.log(occurrences); // Output: 2
Certainly! Let's continue using the same input string for these new exercises, and this time we'll explore different ES6 string methods.
Input (common for all exercises):
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
More...
Exercise 1:
Task: Get the character at index 13 from the given string using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const character = inputString.charAt(13);
console.log(character); // Output: 's'
Exercise 2:
Task: Check if the given string contains only alphabetic characters using an ES6 string method.
Solution:
const inputString = 'HelloTypescriptThisisateststringforTypescriptexercises';
const result = inputString.match(/^[a-zA-Z]+$/) !== null;
console.log(result); // Output: true
Exercise 3:
Task: Split the given string into an array of substrings using the comma as the separator using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.split(',');
console.log(result); // Output: ['Hello', ' Typescript! This is a test string for Typescript exercises.']
Exercise 4:
Task: Check if the given string contains the word "test" using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const result = inputString.includes('test');
console.log(result); // Output: true
Exercise 5:
Task: Repeat the given string three times, concatenating the repetitions using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const repeatedString = inputString.repeat(3);
console.log(repeatedString);
// Output: 'Hello, Typescript! This is a test string for Typescript exercises.Hello, Typescript! This is a test string for Typescript exercises.Hello, Typescript! This is a test string for Typescript exercises.'
Exercise 6:
Task: Find the first occurrence of the letter 'a' in the given string using an ES6 string method.
Solution:
const inputString = 'Hello, Typescript! This is a test string for Typescript exercises.';
const index = inputString.indexOf('a');
console.log(index); // Output: 13
Exercise 7:
Task: Check if the given string is empty using an ES6 string method.
Solution:
const inputString = '';
const result = inputString.length === 0;
console.log(result); // Output: true
Regex Methods
Input (common for all exercises):
const inputString = 'The quick brown fox jumps over the lazy dog.';
Exercise 1:
Task: Check if the given string contains the word "fox" using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /fox/;
const result = regex.test(inputString);
console.log(result); // Output: true
Exercise 2:
Task: Find the first occurrence of the word "the" in the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /\bthe\b/;
const match = inputString.match(regex);
console.log(match?.index); // Output: 4
Exercise 3:
Task: Extract all the words from the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /\b\w+\b/g;
const words = inputString.match(regex);
console.log(words); // Output: ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
Exercise 4:
Task: Replace all occurrences of the word "the" with "a" in the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /\bthe\b/g;
const replacedString = inputString.replace(regex, 'a');
console.log(replacedString);
// Output: 'a quick brown fox jumps over a lazy dog.'
Exercise 5:
Task: Check if the given string starts with "The" using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /^The/;
const result = regex.test(inputString);
console.log(result); // Output: true
Exercise 6:
Task: Check if the given string ends with "dog" using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /dog$/;
const result = regex.test(inputString);
console.log(result); // Output: true
Exercise 7:
Task: Count the number of occurrences of the word "the" in the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /\bthe\b/g;
const occurrences = (inputString.match(regex) || []).length;
console.log(occurrences); // Output: 2
More...
Sure, let's dive into some more complex TypeScript regex exercises. Remember, "complex" doesn't necessarily mean long solutions, but exercises that involve more intricate patterns or multiple steps.
Input (common for all exercises):
const inputString = 'The quick brown fox jumps over the lazy dog. The dog barks.';
Exercise 1:
Task: Find all occurrences of words starting with "d" or "b" in the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog. The dog barks.';
const regex = /\b[b|d]\w+\b/gi;
const matches = inputString.match(regex);
console.log(matches); // Output: ['brown', 'dog', 'dog', 'barks']
Exercise 2:
Task: Extract all sentences from the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog. The dog barks.';
const regex = /[A-Z].+?[.!?]/g;
const sentences = inputString.match(regex);
console.log(sentences);
// Output: ['The quick brown fox jumps over the lazy dog.', 'The dog barks.']
Exercise 3:
Task: Replace all vowels in the given string with underscores using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /[aeiou]/gi;
const replacedString = inputString.replace(regex, '_');
console.log(replacedString);
// Output: 'Th_ q__ck br_wn f_x j_mps _v_r th_ l_zy d_g.'
Exercise 4:
Task: Check if the given string contains three consecutive vowels using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /[aeiou]{3}/i;
const result = regex.test(inputString);
console.log(result); // Output: true
Exercise 5:
Task: Extract all capitalized words from the given string using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /\b[A-Z]\w*\b/g;
const capitalizedWords = inputString.match(regex);
console.log(capitalizedWords); // Output: ['The']
Exercise 6:
Task: Check if the given string contains "fox" or "dog" but not both using an ES6 regex method.
Solution:
const inputString = 'The quick brown fox jumps over the lazy dog.';
const regex = /(?:fox|dog)(?!.*(?:fox|dog))/i;
const result = regex.test(inputString);
console.log(result); // Output: true