forked from ironhack-labs/lab-javascript-basic-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
189 lines (141 loc) · 5.9 KB
/
Copy pathindex.js
File metadata and controls
189 lines (141 loc) · 5.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Iteration 1: Names and Input
//Driver
let hacker1 = "Group";
console.log(`The driver's name is ${hacker1}`);
//Navigator
let hacker2 = "AGroup_Nav";
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((hacker1.length < hacker2.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, ${hacker2.length} characters!`);
}
// Iteration 3: Loops
//3.1
let NewName = "";
for(let i=0; i<(hacker1.length);i++){
NewName = NewName + hacker1[i].toUpperCase();
NewName = NewName + " ";
}
console.log(NewName);
//3.2
let Reversed = "";
for(let i=(hacker2.length)-1; i>=0;i--){
Reversed += hacker2[i];
}
console.log(Reversed);
//3.3
//way1
const abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let cnt=0;
let indicador=false;
while(indicador===false){
//cnt++;
if((hacker1[cnt]===undefined)&&(hacker2[cnt]!==undefined)){
console.log("The driver's name goes first.");
break;
}
else if((hacker2[cnt]===undefined)&&(hacker1[cnt]!==undefined)){
console.log("Yo, the navigator goes first, definitely.");
break;
}
else if((hacker2[cnt]===undefined)&&(hacker1[cnt]===undefined)){
console.log("What?! You both have the same name?");
break;
}
for(let i=0;i<abc.length;i++){
if((hacker1[cnt].toUpperCase()===abc[i])&&(hacker2[cnt]===abc[i])||((hacker1[cnt].toUpperCase()!==abc[i])&&(hacker2[cnt]!==abc[i]))){
cnt++;
break;
}
else if((hacker1[cnt].toUpperCase()===abc[i])&&(hacker2[cnt]!==abc[i])){
console.log("The driver's name goes first.");
indicador=true;
break;
}
else if((hacker1[cnt].toUpperCase()!==abc[i])&&(hacker2[cnt]===abc[i])){
console.log("Yo, the navigator goes first, definitely.");
indicador=true;
break;
}
}
}
//way 2
let iterateHackName=0;
compare=[undefined,"A"];
while(true){
compare= [hacker1[iterateHackName], hacker2[iterateHackName]];
compare.sort();
if((compare[0]===hacker1[iterateHackName])&&(compare[0]!==hacker2[iterateHackName])){
console.log("The driver's name goes first.");
break;
}
else if((compare[0]!==hacker1[iterateHackName])&&(compare[0]===hacker2[iterateHackName])){
console.log("Yo, the navigator goes first, definitely.");
break;
}
else if((compare[0]===hacker1[iterateHackName])&&(compare[0]===hacker2[iterateHackName])){
iterateHackName++;
}
if((hacker1===undefined)||(hacker2===undefined)){
break;
}
}
//Bonus 1
let longText= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse tempus sapien non mauris commodo, ut consectetur nisl efficitur. Sed ultricies leo pharetra, sagittis eros in, tempus metus. Suspendisse quam ex, bibendum sed tincidunt nec, facilisis id quam. Praesent varius fermentum blandit. Morbi vel risus luctus, viverra arcu vitae, euismod turpis. Donec sed magna suscipit, gravida nibh eu, viverra sapien. Integer faucibus sem id lorem auctor pulvinar. Phasellus suscipit vestibulum eros, sed convallis nibh semper eu. Quisque iaculis ac velit a interdum. Maecenas leo mauris, egestas nec mauris ut, gravida elementum magna. Proin tempus aliquet ligula, ac semper eros suscipit sed. Donec id tellus lorem. In nec lorem id ex semper maximus non id nisi. Sed purus erat, bibendum id mollis nec, tincidunt sit amet dolor. Suspendisse interdum orci sem, et imperdiet ipsum hendrerit sit amet. Donec vitae consequat arcu. Duis sapien turpis, imperdiet vitae arcu vel, suscipit fringilla ligula. Vivamus pharetra lorem quis odio luctus, porttitor egestas nunc commodo. Duis imperdiet diam a iaculis interdum. Morbi vitae congue turpis. Mauris vulputate vulputate lacus, in pretium tortor. In dictum ac neque quis venenatis. Praesent lacinia lacinia purus, a dignissim felis dapibus in. Nulla malesuada maximus ipsum non euismod. Duis semper commodo quam non finibus. Praesent quis ullamcorper felis. Maecenas dignissim sodales nisi et lacinia. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nam at mi viverra, varius ex in, laoreet justo. In auctor rutrum dui, non interdum nulla vehicula in.";
let arrayWords=[""];
let WordCount=0;
let flag=0;
let indexArray=0;
for(let i=0; i<longText.length;i++ ){
if(longText[i]!== " "){
flag=1;
arrayWords[indexArray]=arrayWords[indexArray]+ longText[i];
}
else if(longText[i] === " " && (flag===1)){
indexArray++;
arrayWords[indexArray]="";
flag=0;
}
}
//console.log(arrayWords.length);
for(let i=0;i<arrayWords.length;i++){
if(arrayWords[i]==="et"){
WordCount++;
}
}
console.log(WordCount);
//Bonus 2
let phraseToCheck="!!nnbNNq-";
let phraseToCheckFiltered="";
let phraseToCheckRev="";
const ABCdar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
console.log(ABCdar.length);
let cntcnt=0
for(let i = 0;i<phraseToCheck.length;i++){
for(let j=0;j<ABCdar.length;j++){
if(phraseToCheck[i].toUpperCase() === ABCdar[j].toUpperCase()){
phraseToCheckFiltered+=phraseToCheck[i];
}
//console.log("dentro");
}
//console.log("fora");
}
//console.log("\n");
console.log(phraseToCheckFiltered);
//console.log("!".toUpperCase === "A");
for(let i = phraseToCheckFiltered.length-1;i>=0;i--){
phraseToCheckRev+=phraseToCheckFiltered[i];
}
console.log(phraseToCheckRev);
if(phraseToCheckRev.toUpperCase()===phraseToCheckFiltered.toUpperCase()){
console.log(`It is a palindrome: ${phraseToCheck}`);
}
else{
console.log(`It is not a palindrome: ${phraseToCheck}`);
}