From 2d60a2bdcaf2abf69572276430faa68d319e264d Mon Sep 17 00:00:00 2001 From: ramya Date: Tue, 18 Mar 2025 18:17:04 -0500 Subject: [PATCH] practice string methods --- common-string-methods.js | 150 ++++++++++++++------------------------- 1 file changed, 53 insertions(+), 97 deletions(-) diff --git a/common-string-methods.js b/common-string-methods.js index f6e2c29..5331b0c 100644 --- a/common-string-methods.js +++ b/common-string-methods.js @@ -1,103 +1,59 @@ -/* - -Practice Problem #1 - -Activity 1: Searching Strings -Write a program to: -Check if the text "JavaScript" is in the string "Learning JavaScript is fun!" using includes. -Find the position of the word "fun" in the string. - -Activity 2: Transforming Strings -Convert the string " CODE BOOTCAMP " to lowercase and remove all extra whitespace. -Replace "BOOTCAMP" with "JavaScript" in the transformed string. - -Activity 3: Breaking Apart a Sentence -Split the sentence "Coding is fun and educational" into an array of words. - -Activity 4: Retrieving Substrings -Retrieve the first character of "Bootcamp" using charAt. -Extract the word "camp" from "Bootcamp" using slice. - -Advanced Challenge -Write a program to process the following string: -Customer: John Doe -Order: Apple, Banana, Grape -Total: $20.50 -Extract the customer name. -Split the order into an array of items. -Convert the total price to uppercase (e.g., "TOTAL: $20.50"). - - -Practice Problem #2 - -Objective -Practice using common string methods to manipulate and extract information from strings. - -Instructions: -You are tasked with processing a single string and -performing a series of operations using the string methods covered in the lesson. -Each task corresponds to one or more methods and can be completed independently. - -String to Use: - let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. "; - -Tasks: -Complete the following tasks and assign the results to the specified variables. Log each result to the console. - -1. Searching - - Check if the word "JavaScript" is in the string using includes and assign the result to a variable named hasJavaScript. - - Find the position of the word "Coding" in the string using indexOf and assign the result to a variable named codingPosition. - - Check if the string starts with "Welcome" using startsWith and assign the result to a variable named startsWithWelcome. - - Check if the string ends with "today." using endsWith and assign the result to a variable named endsWithToday. - -2. Transforming - - Convert the string to all lowercase letters using toLowerCase and assign the result to a variable named lowercaseString. - - Convert the string to all uppercase letters using toUpperCase and assign the result to a variable named uppercaseString. - - Remove the extra spaces from the beginning and end of the string using trim and assign the result to a variable named trimmedString. - - Replace the word "JavaScript" with "coding" using replace and assign the result to a variable named replacedString. - -3. Breaking Apart - - Split the string into an array of words using split with a space (" ") as the delimiter and assign the result to a variable named wordsArray. - -4. Retrieving - - Retrieve the first character of the trimmed string using charAt and assign the result to a variable named firstCharacter. - - Extract the word "Bootcamp" from the string using slice and assign the result to a variable named extractedBootcamp. - -*/ - -//Starter Code +//String to Use let inputString = " Welcome to the Coding Bootcamp! Learn JavaScript today. "; -// 1. Searching -let hasJavaScript; // Your code here -let codingPosition; // Your code here -let startsWithWelcome; // Your code here -let endsWithToday; // Your code here - -// 2. Transforming -let lowercaseString; // Your code here -let uppercaseString; // Your code here -let trimmedString; // Your code here -let replacedString; // Your code here +// 1.1 Searching +let hasJavaScript = inputString.includes("JavaScript"); +if(hasJavaScript) { + console.log("Provided input string contains word JavaScript") +} else{ + console.log("Provided input string doesn't contains word JavaScript") +} + +//1.2 Finding the indexOf of a word "Coding" +let codingPosition = inputString.indexOf("Coding"); +console.log("Index of the word coding in the provided input string is: " + codingPosition); + +// 1.3 Checking if the string starts with word "Welcome" +let startsWithWelcome = inputString.startsWith("Welcome"); +if(startsWithWelcome) { + console.log("Provided input string starts with word Welcome"); +} else{ + console.log("Provided input string doesn't starts with word Welcome"); +} + +//1.4 checking +let endsWithToday = inputString.endsWith("today."); +if(endsWithToday) { + console.log("Provided input string ends with word today."); +} else{ + console.log("Provided input string doesn't end with word today."); +} + +// 2.1 lower case +let lowercaseString = inputString.toLowerCase(); +console.log("Provided input string in lower case: " + lowercaseString); + +// 2.2 upper case +let uppercaseString = inputString.toUpperCase(); +console.log("Provided input string in upper case: " + uppercaseString); + +//2.3 trimming extra spaces +let trimmedString = inputString.trim(" "); +console.log("Provided input string after trimming the extra spaces: " + trimmedString); + +//2.4 replace +let replacedString = inputString.replace("JavaScript","coding"); +console.log("Provided input string after replacing the JavaScript word with coding: " + replacedString); // 3. Breaking Apart -let wordsArray; // Your code here +let wordsArray = inputString.split(" "); +console.log("Provided input string after breaking apart: "); +console.log(wordsArray); -// 4. Retrieving -let firstCharacter; // Your code here -let extractedBootcamp; // Your code here +// 4.1 Retrieving character of the trimmed string +let firstCharacter = inputString.trim(" ").charAt(0); +console.log("first character of the trimmed input string is: " + firstCharacter); -// Log all results -console.log({ - hasJavaScript, - codingPosition, - startsWithWelcome, - endsWithToday, - lowercaseString, - uppercaseString, - trimmedString, - replacedString, - wordsArray, - firstCharacter, - extractedBootcamp, -}); +//4.2 Extracting the word Bootcamp from the input string +let extractedBootcamp = inputString.slice(24,32); +console.log("Extracted word from the provided input string is: " + extractedBootcamp); \ No newline at end of file