|
| 1 | +/** @format */ |
| 2 | + |
1 | 3 | // Iteration 1: Names and Input
|
| 4 | +const hacker1 = "Greg"; // 1.1 |
| 5 | +const hacker2 = "Viktor"; // 1.2 |
2 | 6 |
|
| 7 | +console.log(`The driver's name is ${hacker1}`); // 1.3 |
| 8 | +console.log(`The navigator's name is ${hacker2}`); // 1.4 |
3 | 9 |
|
4 | 10 | // Iteration 2: Conditionals
|
5 | 11 |
|
| 12 | +//2.1 |
| 13 | + |
| 14 | +const hacker1Length = hacker1.length; |
| 15 | +const hacker2Length = hacker2.length; |
| 16 | + |
| 17 | +if (hacker1Length > hacker2Length) { |
| 18 | + console.log( |
| 19 | + `The driver has the longest name, it has ${hacker1Length} characters.` |
| 20 | + ); |
| 21 | +} else if (hacker2Length > hacker1Length) { |
| 22 | + console.log( |
| 23 | + `It seems that the navigator has the longest name, it has ${hacker2Length} characters.` |
| 24 | + ); |
| 25 | +} else { |
| 26 | + console.log( |
| 27 | + `Wow, you both have equally long names, ${hacker2Length} characters.` |
| 28 | + ); |
| 29 | +} |
6 | 30 |
|
7 | 31 | // Iteration 3: Loops
|
| 32 | + |
| 33 | +// 3.1 |
| 34 | + |
| 35 | +let hacker1Name = ""; |
| 36 | +for (let i = 0; i < hacker1Length; i++) { |
| 37 | + hacker1Name += hacker1[i].toUpperCase() + " "; |
| 38 | +} |
| 39 | +console.log(hacker1Name); |
| 40 | + |
| 41 | +// 3.2 |
| 42 | + |
| 43 | +let hacker2Name = ""; |
| 44 | + |
| 45 | +for (let i = hacker2Length - 1; i >= 0; i--) { |
| 46 | + hacker2Name += hacker2[i]; |
| 47 | +} |
| 48 | +console.log(hacker2Name); |
| 49 | + |
| 50 | +// 3.3 locale.compare |
| 51 | + |
| 52 | +const comparison = hacker1.localeCompare(`${hacker2}`); |
| 53 | +if (comparison < 0) { |
| 54 | + console.log("The driver's name goes first."); |
| 55 | +} else if (comparison > 0) { |
| 56 | + console.log("Yo, the navigator goes first, definitely."); |
| 57 | +} else { |
| 58 | + console.log("What?! You both have the same name?"); |
| 59 | +} |
0 commit comments