-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (102 loc) · 2.45 KB
/
index.js
File metadata and controls
140 lines (102 loc) · 2.45 KB
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
// Iteration 1: Names and Input
let hacker1 = "Ithaisa";
console.log("The driver's name is " + hacker1);
let hacker2 = "Alexander";
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 +
hacker2.length +
" characters",
);
}
// Iteration 3: Loops
//3.1
let i = 0;
let result = "";
while (i < hacker1.length) {
result += hacker1[i].toUpperCase() + " ";
i++;
}
console.log(result);
//3.2
let j = hacker2.length - 1;
let reverse = "";
while (j >= 0) {
reverse += hacker2[j];
j--;
}
console.log(reverse);
//3.3
let k = 0;
let found = false;
while (k < hacker1.length && k < hacker2.length && !found) {
if (hacker1[k] < hacker2[k]) {
console.log("The driver's name goes first.");
found = true;
} else if (hacker1[k] > hacker2[k]) {
console.log("Yo, the navigator goes first, definitely.");
found = true;
}
k++;
}
if (!found) {
if (hacker1.length > hacker2.length) {
console.log("Yo, the navigator goes first, definitely.");
} else if (hacker1.length < hacker2.length) {
console.log("The driver's name goes first.");
} else {
console.log("What?! You both have the same name?");
}
}
//Bonus 1:
const longText =
"Lorem ipsum et dolor sit amet et consectetur adipiscing elit et sed do eiusmod tempor";
let words = 0;
for (let i = 0; i < longText.length; i++) {
if (longText[i] === " ") {
words++;
}
}
words = words + 1;
console.log("Number of words: " + words);
let countEt = 0;
for (let i = 0; i < longText.length - 1; i++) {
if (longText[i] === "e" && longText[i + 1] === "t") {
countEt++;
}
}
console.log("Number of et: " + countEt);
//Bonus 2:
let phraseToCheck = "amor roma";
let clean = "";
let indice = 0;
while (indice < phraseToCheck.length) {
if (phraseToCheck[i] !== " ") {
clean += phraseToCheck[i].toLowerCase();
}
indice++;
}
let reversee= "";
let jota = clean.length - 1;
while (jota >= 0) {
reversee += clean[j];
jota--;
}
if (clean === reversee) {
console.log("Es palindromo");
} else {
console.log("No es palindromo");
}