From e22a4d5acdc5d5e43f63f553c5d0293433d3f6cc Mon Sep 17 00:00:00 2001 From: Ramix76 Date: Tue, 17 Jun 2025 13:18:08 +0200 Subject: [PATCH] lab-3-2-2 done --- index.js | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/index.js b/index.js index 6b0fec3ad..56ef99f2a 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,108 @@ // Iteration 1: Names and Input +const hacker1 = "Ferran"; +console.log("The driver's name is " + hacker1); + + +const hacker2 = "Ramos"; +console.log("The navigator's name is " + hacker2); + // Iteration 2: Conditionals +const name1 = hacker1.length; +const name2 = hacker2.length; + +if (name1 > name2) { + console.log("The driver has the longest name, it has " + name1 + " characters."); + document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

The driver has the longest name, it has ${name1} characters.

+ `; +} else if (name1 < name2) { + console.log("It seems that the navigator has the longest name, it has " + name2 + " characters."); + document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

It seems that the navigator has the longest name, it has ${name2} characters.

+ `; +} else { + console.log("Wow, you both have equally long names, XX characters!."); + document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

Wow, you both have equally long names, it has ${name2} characters.

+ `; +} + // Iteration 3: Loops + +let res = ""; + +for (let i = 0; i < hacker1.length; i++) { + res += hacker1[i].toUpperCase(); + if (i < hacker1.length - 1) + res += " "; +} + +console.log(res); +document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

Wow, you both have equally long names, it has ${name2} characters.

+

${res}

+ `; + +let result = ""; + +for (let i = hacker2.length - 1; i >= 0; i--) { + result += hacker2[i]; +} + +console.log(result); +document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

Wow, you both have equally long names, it has ${name2} characters.

+

${res}

+

${result} + `; + + +let compare = ""; + +const minLength = Math.min(hacker1.length, hacker2.length); + +for (let i = 0; i < minLength; i++) { + const result = hacker1[i].localeCompare(hacker2[i]); + + if (result < 0) { + compare = "The driver's name goes first."; + break; + } else if (result > 0) { + compare = "Yo, the navigator goes first, definitely."; + break; + } +} + +if (compare === "") { + if (hacker1.length < hacker2.length) { + compare = "The driver's name goes first."; + } else if (hacker1.length > hacker2.length) { + compare = "Yo, the navigator goes first, definitely."; + } else { + compare = "What?! You both have the same name?"; + } +} + +console.log(compare); +document.body.innerHTML = ` +

The driver's name is ${hacker1}

+

The navigator's name is ${hacker2}

+

Wow, you both have equally long names, it has ${name2} characters.

+

${res}

+

${result}

+

${compare}

+ `; \ No newline at end of file