From 1ffb122ac34e7115c90d13bc9bfd4241bf4d2943 Mon Sep 17 00:00:00 2001 From: Oleksandr Bakai Date: Sat, 14 Jun 2025 15:40:59 +0200 Subject: [PATCH] NameLoops --- index.html | 3 ++- index.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 0758034e6..05443fdca 100644 --- a/index.html +++ b/index.html @@ -4,6 +4,7 @@ LAB | JS Basic Algorithms +

LAB | JS Basic Algorithms

@@ -11,6 +12,6 @@

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..cc1766368 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,38 @@ -// Iteration 1: Names and Input +let hacker1 = prompt("Enter your name hacker1"); //The driver's name is XXXX +let space = ""; +for(let i = hacker1.length -1; i>=0; i--){//reverse name with spaces + space +=hacker1[i].toUpperCase()+" "; +} +alert(space.trim()); -// Iteration 2: Conditionals +let hacker2 = prompt("Enter your name hacker2"); //The navigator's name is YYYY +let space2 = ""; +for(let i = hacker2.length -1; i>=0; i--){//reverse name with spaces + space2 +=hacker2[i].toUpperCase()+" "; +} +alert(space2.trim()); -// Iteration 3: Loops +//Longest name +if(hacker1.length > hacker2.length){ + alert(`Hacker1 has longer name it has ${hacker1.length} characters.`); +} +else if (hacker1.length < hacker2.length){ + alert(`Hacker2 has longer name it has ${hacker2.length} characters.`); + } +else{ + alert(`Wow, you both have equally long names, ${hacker1.length} characters!`); + } + + +//Alphabeticaly first +if (hacker1hacker2){ + alert(`The ${hacker2.toUpperCase()} is alphabeticaly goes first.`); +} +else{ + alert("You have the same name!"); +}