From 1f49c03de0ec9f7e060f26af0dae0fba991e40b7 Mon Sep 17 00:00:00 2001 From: Kara Jelley Date: Wed, 16 Oct 2024 09:25:18 -0400 Subject: [PATCH] Solved lab --- index.html | 2 ++ index.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/index.html b/index.html index 0758034e6..a784fe44e 100644 --- a/index.html +++ b/index.html @@ -12,5 +12,7 @@

LAB | JS Basic Algorithms

Open the Dev Tools console to see the console output.

+ + \ No newline at end of file diff --git a/index.js b/index.js index 6b0fec3ad..1e08c55d8 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,59 @@ // Iteration 1: Names and Input +let hacker1 = "john"; + +console.log(`The driver's name is ${hacker1}`); + +let hacker2 = "maria"; + +console.log(`The navigator's name is ${hacker2}`); + + + // Iteration 2: Conditionals +if (hacker1.length > hacker2.length) { + console.log(`${hacker1} has the longest name, it has ${hacker1.length} characters.`) +} else if (hacker2.length > hacker1.length){ + console.log(`It seems that ${hacker2} 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 +let hacker1Upper = hacker1.toUpperCase() +let finalString = ""; + + for (let i = 0; i < hacker1Upper.length; i++) { + finalString += hacker1Upper[i] + " "; + +} + +console.log(finalString); + + +//3.2 +let finalString2 = ""; + +for (let i = hacker2.length - 1; i >= 0; i--) { + finalString2 += hacker2[i]; +} + +console.log(finalString2); + + +//3.3 + + +if (hacker1.localeCompare(hacker2) === -1) { + console.log(`${hacker1} goes first.`); +} else if (hacker1.localeCompare(hacker2) === 1) { + console.log(`Yo, ${hacker2}goes first, definitely.`); +} else { + console.log(`What?! You both have the same name?`); +} +