From e1d3fb6b24751756a53932a7ad460526087bbb37 Mon Sep 17 00:00:00 2001 From: Nikita Shah Date: Mon, 25 Nov 2024 16:45:14 +0530 Subject: [PATCH 1/5] Adding changes for Iteration 1 - Names and Input. --- js/index.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/js/index.js b/js/index.js index 59e4af7..fca698c 100644 --- a/js/index.js +++ b/js/index.js @@ -1,5 +1,21 @@ // Iteration 1: Names and Input -// + +// 1.1 Create a variable hacker1 with the driver's name. +let hacker1 = "Edward Cullen"; + +// 1.2 Print "The driver's name is XXXX". +console.log(`The driver's name is ${hacker1}.`); + +// 1.3 Create a variable hacker2 with the navigator's name. +let hacker2 = "Jacob Black"; + +// 1.4 Print "The navigator's name is YYYY". +console.log(`The navigator's name is ${hacker2}.`); + +// ========== End of Iteration 1 ========== // + + + // Iteration 2: Conditionals From 7dd2d70fa75360e5ff648f0bc6aae62e18a97dc4 Mon Sep 17 00:00:00 2001 From: Nikita Shah Date: Mon, 25 Nov 2024 17:01:57 +0530 Subject: [PATCH 2/5] Adding changes for Iteration 2 - Conditionals. --- js/index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/js/index.js b/js/index.js index fca698c..4a08a11 100644 --- a/js/index.js +++ b/js/index.js @@ -18,5 +18,26 @@ console.log(`The navigator's name is ${hacker2}.`); // Iteration 2: Conditionals +/* +2.1. Depending on which name is longer, print: + • The driver has the longest name, it has XX characters. or + • It seems that the navigator has the longest name, it has XX characters. or + • Wow, you both have equally long names, XX characters!. +*/ + +let hacker1Length = hacker1.length; +let hacker2Length = hacker2.length; + +if (hacker1Length > hacker2Length) { + console.log(`The driver has the longest name, it has ${hacker1Length} characters.`); +} else if (hacker1Length < hacker2Length) { + console.log(`It seems the navigator has the longest name, it has ${hacker2Length} characters.`); +} else { + console.log(`Wow, you both have equally long names, ${hacker1Length} characters!`); +} + +// ========== End of Iteration 2 ========== // + + // Iteration 3: Loops From d5e50e815a1837a0b7db9dd684553de3ca161b8f Mon Sep 17 00:00:00 2001 From: Nikita Shah Date: Mon, 25 Nov 2024 18:05:46 +0530 Subject: [PATCH 3/5] Adding changes for Iteration 3 - Loops. --- js/index.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/js/index.js b/js/index.js index 4a08a11..efdd40f 100644 --- a/js/index.js +++ b/js/index.js @@ -20,9 +20,9 @@ console.log(`The navigator's name is ${hacker2}.`); /* 2.1. Depending on which name is longer, print: - • The driver has the longest name, it has XX characters. or - • It seems that the navigator has the longest name, it has XX characters. or - • Wow, you both have equally long names, XX characters!. + 1. The driver has the longest name, it has XX characters. or + 2. It seems that the navigator has the longest name, it has XX characters. or + 3. Wow, you both have equally long names, XX characters!. */ let hacker1Length = hacker1.length; @@ -41,3 +41,51 @@ if (hacker1Length > hacker2Length) { // Iteration 3: Loops + +/* +3.1 Print all the characters of the driver's name, separated by a space and in capitals +i.e. "J O H N" +*/ + +let driverName = ""; + +for (let i = 0; i < hacker1Length; i++) { + if (hacker1.charAt(i) !== " ") { + driverName += hacker1.charAt(i).toUpperCase(); + if (i < hacker1Length - 1) { + driverName += " "; + } + } +} + +console.log(driverName); + +/* +3.2 Print all the characters of the navigator's name, in reverse order. +i.e. "nhoJ" +*/ + +let navigatorName = ""; + +for (let j = hacker2Length -1; j >= 0; j--) { + navigatorName += hacker2[j]; +} + +console.log(navigatorName); + +/* +3.3 Depending on the lexicographic order of the strings, print: + 1. The driver's name goes first. + 2. Yo, the navigator goes first definitely. + 3. What?! You both have the same name? +*/ + +if (hacker1 > hacker2) { + console.log("The driver's name goes first."); +} else if (hacker1 < hacker2) { + console.log("Yo, the navigator goes first definitely.") +} else { + console.log("What?! You both have the same name?"); +} + +// ========== End of Iteration 3 ========== // \ No newline at end of file From 6c15958af413464832390ec7184cb4ca4439ad6b Mon Sep 17 00:00:00 2001 From: Nikita Shah Date: Wed, 27 Nov 2024 14:50:48 +0530 Subject: [PATCH 4/5] Adding changes for Bonus 1. --- js/index.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/js/index.js b/js/index.js index efdd40f..fd33565 100644 --- a/js/index.js +++ b/js/index.js @@ -88,4 +88,58 @@ if (hacker1 > hacker2) { console.log("What?! You both have the same name?"); } -// ========== End of Iteration 3 ========== // \ No newline at end of file +// ========== End of Iteration 3 ========== // + + + +// ========== Bonus 1 ========== // + +/* +Go to lorem ipsum generator and: +• Generate 3 paragraphs. Store the text in a variable type of string. +• Make your program count the number of words in the string. +• Make your program count the number of times the Latin word et appears. +*/ + +// Step 1: Store the Lorem Ipsum text in a string variable +let loremText = ` +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam at et dui. +Pellentesque et facilisis ligula. Ut pharetra, nisi a fermentum aliquet, +et volutpat nisi sapien id purus. Sed et lectus lorem. + +Vestibulum et orci eu metus malesuada tincidunt. Etiam vel nisl at lacus facilisis euismod. +Maecenas et elit varius, hendrerit nunc sed, viverra est. Duis et nisi vitae felis consectetur pharetra. + +Curabitur at metus sit amet tortor viverra luctus et nec elit. Integer id nibh et magna sollicitudin iaculis. +Suspendisse potenti. Fusce eget et purus a massa lacinia mollis. Vivamus et magna arcu. +`; + +// Step 2: Initialize counters +let wordCount = 0; +let etCount = 0; +let currentWord = ""; + +// Step 3: Loop through the text character by character +for (let i = 0; i <= loremText.length; i++) { + let char = loremText[i]; + + // If we encounter a space, punctuation, or end of string, process the word + if (char === " " || char === "\n" || char === "\t" || i === loremText.length || /[.,!?]/.test(char)) { + if (currentWord) { + wordCount++; // Increment the word count + if (currentWord.toLowerCase() === "et") { + etCount++; // Increment the "et" count if it's a match + } + currentWord = ""; // Reset the word + } + } else { + // Build the current word + currentWord += char; + } +} + +// Step 4: Output the results +console.log(`Word Count: ${wordCount}`); +console.log(`Occurrences of "et": ${etCount}`); + +// ========== End of Bonus 1 ========== // \ No newline at end of file From 810dd7dc141cacb1bbf0b4d768018ad8293a6354 Mon Sep 17 00:00:00 2001 From: Nikita Shah Date: Wed, 27 Nov 2024 22:14:32 +0530 Subject: [PATCH 5/5] Adding changes for Bonus 2. --- js/index.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/js/index.js b/js/index.js index fd33565..6751304 100644 --- a/js/index.js +++ b/js/index.js @@ -142,4 +142,63 @@ for (let i = 0; i <= loremText.length; i++) { console.log(`Word Count: ${wordCount}`); console.log(`Occurrences of "et": ${etCount}`); -// ========== End of Bonus 1 ========== // \ No newline at end of file +// ========== End of Bonus 1 ========== // + + + +// ========== Bonus 2 ========== // + +/* +Create a new variable phraseToCheck and have it contain some string value. Write a code that will check if the value we assigned to this variable is a Palindrome. Here are some examples of palindromes: +"A man, a plan, a canal, Panama!" +"Amor, Roma" +"race car" +"stack cats" +"step on no pets" +"taco cat" +"put it up" +"Was it a car or a cat I saw?" and "No 'x' in Nixon". +*/ + +// Step 1: Initialize the variable +let phraseToCheck = "A man, a plan, a canal, Panama!"; + +// Step 2: Checking if a string is palindrome or not + +// Normalize the string: remove non-alphanumeric characters and convert to lowercase +let cleanedPhrase = ""; + +for (let i = phraseToCheck.length - 1; i >= 0; i--) { + let temp = phraseToCheck[i]; + if (temp === " " || temp === "\n" || temp === "\t" || i === phraseToCheck.length || /[.,!?]/.test(temp)) { + continue; + } else { + cleanedPhrase += temp.toLowerCase(); + } +} + +// Use two pointers to compare characters from the start and end +let left = 0; + +let right = cleanedPhrase.length - 1; + +let palindrome = true; + +while(left < right) { + if (cleanedPhrase[left] !== cleanedPhrase[right]) { + palindrome = false; + break; + } else { + left++; + right--; + continue; + } +} + +// If we complete the loop without mismatches, it's a palindrome +if (palindrome === true) { + console.log(`"${phraseToCheck}" is a palindrome string.`); +} else { + console.log(`"${phraseToCheck}" is not a palindrom string.`); +} +// ========== End of Bonus 2 ========== //