forked from ironhack-labs/lab-javascript-basic-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (114 loc) · 5.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Iteration 1: Names and Input
const hacker1 = "David";
const hacker2 = "Sara";
console.log(`The driver's name is ${hacker1}`);
console.log(`The navigator's name is ${hacker2}`);
// Iteration 2: Conditionals
if (hacker1.length > hacker2.length) {
console.log(
`The driver has the longest name, it has ${hacker1.length} characters.`
);
} else if (hacker2.length > hacker1.length) {
console.log(
`It seems that the navigator 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 Print the characters of the driver's name, separated by space, and in capital letters
let hackerSpace = "";
for (let i = 0; i < hacker1.length; i++) {
const hacker1Char = hacker1[i].toUpperCase();
hackerSpace += hacker1Char;
if (i < hacker1.length - 1) {
hackerSpace += " ";
}
}
console.log(hackerSpace);
// 3.2 Print all the characters of the navigator's name in reverse order
let reverseName = "";
for (let i = hacker2.length - 1; i >= 0; i--) {
const hacker2Char = hacker2[i];
reverseName += hacker2Char;
}
console.log(reverseName);
// 3.3 Depending on the lexicographic order of the strings, print
const comparison = hacker1.localeCompare(hacker2);
if (comparison < 0) {
console.log("The driver's name goes first.");
} else if (comparison > 0) {
console.log("Yo, the navigator goes first, definitely.");
} else {
console.log("What?! You both have the same name?");
}
// Bonus Time!
// Bonus 1
const longText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla ullamcorper ligula sit amet iaculis accumsan.
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
Proin dolor tortor, convallis sed pharetra ut, viverra nec quam. Fusce in interdum augue, a pulvinar nibh.
Aenean fringilla nisl vel porta mollis. Maecenas nec accumsan leo, in imperdiet ipsum. In sit amet urna eu
leo accumsan lobortis sit amet eu velit. Sed consectetur lacinia arcu, id porttitor purus porttitor non.
Nam maximus dignissim mauris at vestibulum. Praesent a ante a turpis elementum finibus.
Mauris porta massa vitae velit sodales consectetur. Maecenas blandit orci et nunc tempus, sed pharetra
mauris laoreet. Fusce bibendum a lectus et pellentesque.
Aenean eleifend dolor eros, vitae blandit odio semper eget. Phasellus ut neque laoreet, dignissim dolor sit
amet, fermentum risus. Vestibulum dignissim, velit id pellentesque venenatis, felis orci mollis ex, ac tincidunt
lacus lectus vitae purus. Quisque rutrum rutrum ante, et porta magna finibus ac. Cras sem nisi, egestas
pellentesque pellentesque sed, porta sed nunc. In sollicitudin tellus in malesuada condimentum. Suspendisse
elementum mauris non lobortis sagittis. Vivamus urna odio, venenatis non enim quis, viverra vestibulum eros.
Nunc eros metus, aliquam id diam a, sagittis aliquet quam. Orci varius natoque penatibus et magnis dis parturient
montes, nascetur ridiculus mus. Sed sodales et arcu ac dapibus. Cras non mauris nec metus sagittis convallis
sed nec velit. Aenean erat velit, mattis non interdum in, sodales non lorem. Aliquam finibus urna sit amet erat
efficitur tempor. Phasellus ut ante molestie, mattis tortor nec, viverra lacus. Nullam molestie cursus ex, sed
sollicitudin enim egestas sagittis.
Sed vel finibus dolor. Maecenas ante ipsum, tristique eget suscipit nec, pharetra id nibh. Praesent vehicula leo
a massa consequat, vitae vestibulum est malesuada. Praesent id ex iaculis, feugiat lectus ac, lacinia purus.
Nam id nisl quis turpis ultricies sollicitudin vitae auctor dolor. Sed iaculis ligula in volutpat sodales. Donec
ligula nulla, vehicula quis nunc sit amet, hendrerit dapibus enim. Nulla pharetra nisi in metus aliquet ultrices.
Nam volutpat vel nulla et ullamcorper. Quisque volutpat rutrum nunc at vestibulum.`;
// Count words
const words = longText.split(" ");
const wordCount = words.length;
console.log(wordCount);
// Count number of "et"
let etCount = 0;
for (let i = 0; i < longText.length - 1; i++) {
const char = longText[i];
const nextChar = longText[i + 1];
if (char === "e" && nextChar === "t") {
etCount++;
}
}
console.log(etCount);
// Bonus 2
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
// Ignore non-alphabetic characters from the start
let leftChar = str[left].toLowerCase();
while (left < right && !(leftChar >= "a" && leftChar <= "z")) {
left++;
leftChar = str[left].toLowerCase();
}
// Ignore non-alphabetic characters from the end
let rightChar = str[right].toLowerCase();
while (left < right && !(rightChar >= "A" && rightChar <= "z")) {
right--;
rightChar = str[right].toLowerCase();
}
// Compare the characters
if (leftChar !== rightChar) {
return false;
}
// Move towards the center
left++;
right--;
}
return true;
}
console.log(isPalindrome("A man, a plan, a canal: Panama")); // true
console.log(isPalindrome("This is not a palindrome")); // false