Skip to content

lab-3-2-2 done #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
101 changes: 101 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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 = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>The driver has the longest name, it has ${name1} characters.</p>
`;
} else if (name1 < name2) {
console.log("It seems that the navigator has the longest name, it has " + name2 + " characters.");
document.body.innerHTML = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>It seems that the navigator has the longest name, it has ${name2} characters.</p>
`;
} else {
console.log("Wow, you both have equally long names, XX characters!.");
document.body.innerHTML = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>Wow, you both have equally long names, it has ${name2} characters.</p>
`;
}


// 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 = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>Wow, you both have equally long names, it has ${name2} characters.</p>
<h2>${res}</h2>
`;

let result = "";

for (let i = hacker2.length - 1; i >= 0; i--) {
result += hacker2[i];
}

console.log(result);
document.body.innerHTML = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>Wow, you both have equally long names, it has ${name2} characters.</p>
<h2>${res}</h2>
<h3>${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 = `
<p>The driver's name is ${hacker1}</p>
<p>The navigator's name is ${hacker2}</p>
<p>Wow, you both have equally long names, it has ${name2} characters.</p>
<h2>${res}</h2>
<h3>${result}</h3>
<h4>${compare}</h4>
`;