Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ <h1>LAB | JS Basic Algorithms</h1>
<br />
<p> Open the <a href="https://developer.chrome.com/docs/devtools/open/">Dev Tools console</a> to see the console output.</p>

<script src="index.js"></script>
<script src="./index.js"></script>
</body>
</html>
78 changes: 77 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,83 @@
// Iteration 1: Names and Input
let hacker1 = "Maria";
console.log("The driver's name is " + hacker1);

let hacker2 = "John";
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
console.log(hacker1.toUpperCase().split("").join(" "));
console.log(hacker2.split("").reverse().join(""));

// 3.3 Lexicographic order
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?");
}

// Bonus 1:
let longText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam et sapien euismod, et facilisis nunc ultrices.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.`;

let words = 0;
for(let i = 0; i < longText.length; i++) {
if (longText[i] === " " || longText[i] === "\n"){
words++;
}
}
words += 1;
console.log("Number of words:", words);

let etCount = 0;
let textLower = longText.toLowerCase();

for (let i = 0; i < textLower.length - 1; i++) {
if (textLower[i] === "e" && textLower[i+1] === "t") {
let before = (i === 0) ? " " : textLower[i-1];
let after = (i+2 >= textLower.length) ? " " : textLower[i+2];
if ((before === " " || before === "\n" || before === "," || before === ".") &&
(after === " " || after === "\n" || after === "," || after === ".")) {
etCount++;
}
}
}

console.log("Number of 'et' occurrences:", etCount);

// Bonus 2
let phraseToCheck = "A man, a plan, a canal, Panama!";

let normalized = "";
for (let i = 0; i < phraseToCheck.length; i++) {
let char = phraseToCheck[i].toLowerCase();
if (char >= "a" && char <= "z") {
normalized += char;
}
}

let isPalindrome = true;
for (let i = 0; i < normalized.length / 2; i++) {
if (normalized[i] !== normalized[normalized.length - 1 - i]) {
isPalindrome = false;
break;
}
}

if (isPalindrome) {
console.log("The phrase is a palindrome!");
} else {
console.log("The phrase is NOT a palindrome.");
}