From 9cc3fbed81f56271c2cc04485938d46336a6803a Mon Sep 17 00:00:00 2001 From: Goldmonkey Studio Date: Wed, 21 Jan 2026 00:46:42 +0100 Subject: [PATCH 1/2] Solved lab --- index.js | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6b0fec3ad..0c4763db6 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,41 @@ + // Iteration 1: Names and Input +let hacker1 = "Alice"; +console.log(`The driver's name is ${hacker1}`); +let hacker2 = "Bob"; +console.log(`The navigator's name is ${hacker2}`); // Iteration 2: Conditionals - +if (hacker1.length > hacker2.length) { + console.log(`The driver has the longest name, it has ${hacker1.length} characters.`); +} else if (hacker2.length > hacker1.length) { + console.log(`It seems that the navigator has the longest name, it has ${hacker2.length} characters.`); +} else { + console.log(`Wow, you both have equally long names, ${hacker1.length} characters!`); +} // Iteration 3: Loops +// 3.1 Print driver's name in capital letters, separated by space +let driverNameCaps = ""; +for (let i = 0; i < hacker1.length; i++) { + driverNameCaps += hacker1[i].toUpperCase(); + if (i < hacker1.length - 1) driverNameCaps += " "; +} +console.log(driverNameCaps); + +// 3.2 Print navigator's name in reverse order +let navigatorNameReverse = ""; +for (let i = hacker2.length - 1; i >= 0; i--) { + navigatorNameReverse += hacker2[i]; +} +console.log(navigatorNameReverse); + +// 3.3 Lexicographic order +if (hacker1.localeCompare(hacker2) < 0) { + console.log("The driver's name goes first."); +} else if (hacker1.localeCompare(hacker2) > 0) { + console.log("Yo, the navigator goes first, definitely."); +} else { + console.log("What?! You both have the same name?"); +} From 66d69d08f48718a1f9b723f9f42c7bc5d10fd7e8 Mon Sep 17 00:00:00 2001 From: Goldmonkey Studio Date: Wed, 21 Jan 2026 00:53:15 +0100 Subject: [PATCH 2/2] Implement Bonus 1 and Bonus 2 --- index.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/index.js b/index.js index 0c4763db6..17e819fdf 100644 --- a/index.js +++ b/index.js @@ -31,6 +31,7 @@ for (let i = hacker2.length - 1; i >= 0; i--) { } console.log(navigatorNameReverse); + // 3.3 Lexicographic order if (hacker1.localeCompare(hacker2) < 0) { console.log("The driver's name goes first."); @@ -39,3 +40,53 @@ if (hacker1.localeCompare(hacker2) < 0) { } else { console.log("What?! You both have the same name?"); } + +// Bonus 1 +let longText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam et urna et enim cursus dictum. Etiam euismod, enim et dictum cursus, urna enim cursus enim, et dictum urna enim. Etiam cursus enim et urna dictum, et enim cursus urna. Etiam et cursus enim, dictum urna et enim cursus. Etiam cursus enim et urna dictum, et enim cursus urna.`; + +// Count words +let wordCount = 0; +let inWord = false; +for (let i = 0; i < longText.length; i++) { + if (longText[i] !== ' ' && longText[i] !== '\n') { + if (!inWord) { + wordCount++; + inWord = true; + } + } else { + inWord = false; + } +} +console.log(`Word count: ${wordCount}`); + +// Count 'et' (as a word) +let etCount = 0; +for (let i = 0; i < longText.length - 1; i++) { + // Check for 'et' surrounded by non-letters or at string boundaries + if ( + longText[i] === 'e' && longText[i + 1] === 't' && + (i === 0 || !(/[a-zA-Z]/.test(longText[i - 1]))) && + (i + 2 === longText.length || !(/[a-zA-Z]/.test(longText[i + 2]))) + ) { + etCount++; + } +} +console.log(`'et' count: ${etCount}`); + +// Bonus 2 +let phraseToCheck = "A man, a plan, a canal, Panama!"; +let cleaned = ""; +for (let i = 0; i < phraseToCheck.length; i++) { + let c = phraseToCheck[i].toLowerCase(); + if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') { + cleaned += c; + } +} +let isPalindrome = true; +for (let i = 0, j = cleaned.length - 1; i < j; i++, j--) { + if (cleaned[i] !== cleaned[j]) { + isPalindrome = false; + break; + } +} +console.log(`Is the phrase a palindrome? ${isPalindrome}`);