From 436f4eb6e0c660cb460854137d4cd4f815181f4f Mon Sep 17 00:00:00 2001 From: Almas Khan Date: Sat, 6 Sep 2025 16:26:35 +0200 Subject: [PATCH 1/2] algorithms --- .vscode/settings.json | 3 +++ index.js | 49 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..f673a71b7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "liveServer.settings.port": 5502 +} \ No newline at end of file diff --git a/index.js b/index.js index 6b0fec3ad..461059261 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,52 @@ // Iteration 1: Names and Input - +var hacker1 = "Jackie"; +console.log(` The driver's name is ${hacker1}`); +var hacker2 = "James"; +console.log( ` The navigator's name is ${hacker2}`); // Iteration 2: Conditionals +if (hacker1.length > hacker2.length) { +console.log( `The driver has the longest name ${hacker1.length} characters`); +} +else if (hacker1.length < hacker2.length) { + console.log(` It seems that the navigator has the longest name ${hacker1.length} characters`); - +} +else { + console.log(`Wow, you both have equally long names`) +} // Iteration 3: Loops + +//---capital & Space---// +var updatedName = ""; +for(let i = 0; i < hacker1.length; i++) { + updatedName += hacker1[i].toUpperCase() + " "; +} +console.log(updatedName); + +// const lastIndex = hacker1.length - 1; +// for ( let i = 0; i <= lastIndex; i++) { +// const char = hacker1[i]; +// console.log(char); +// } + + +// ---reverse---// +let newName = ""; +var lastChar = hacker1.length - 1; +for (let i = lastChar; i >= 0; i--) { + newName += hacker1[i]; +} +console.log(newName); +//---// + +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?`) +} \ No newline at end of file From c31f22be468afac4057950b24279633c86dfc364 Mon Sep 17 00:00:00 2001 From: Almas Khan Date: Sun, 7 Sep 2025 16:04:15 +0200 Subject: [PATCH 2/2] dictionary order --- index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 461059261..95d31dc7a 100644 --- a/index.js +++ b/index.js @@ -40,13 +40,15 @@ for (let i = lastChar; i >= 0; i--) { console.log(newName); //---// -if (hacker1 > hacker2) { - console.log( `The driver's name goes first`); -} -else if (hacker1 < hacker2) { - console.log(` Yo, the navigator goes first, definitely.`); +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare + +const result = hacker1.localeCompare(hacker2); +if (result < 0) { + console.log("The driver's name goes first."); +} else if (result > 0) { + console.log("Yo, the navigator goes first, definitely."); +} else { + console.log("What?! You both have the same name?"); } -else { - console.log(`What?! You both have the same name?`) -} \ No newline at end of file +