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
127 lines (111 loc) · 4.64 KB
/
index.js
File metadata and controls
127 lines (111 loc) · 4.64 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
// Iteration 1: Names and Input
function iteration1(name1, name2) {
console.log("Iteration 1");
let hacker1 = name1;
console.log("The driver's name is " + hacker1);
let hacker2 = name2;
console.log(`The navigator's name is ${hacker2}`);
}
// Iteration 2: Conditionals
function iteration2(name1, name2) {
console.log("Iteration 2");
if (name1.length > name2.length) {
console.log(
`The driver has the longest name, it has ${name1.length} characters.`,
);
} else if (name2.length > name1.length) {
console.log(
`It seems that the navigator has the longest name, it has ${name2.length} characters.`,
);
} else {
console.log(
`Wow, you both have equally long names, ${name2.length} characters!.`,
);
}
}
// Iteration 3: Loops
function iteration31(name1, name2) {
console.log("Iteration 3.1");
let response = "";
for (let i = 0; i < name1.length; i++) {
let letter = name1[i];
letter = letter.toUpperCase();
response += letter;
if (i != name1.length - 1) {
response += " ";
}
}
console.log(response);
}
function iteration32(name1, name2) {
console.log("Iteration 3.2");
let response1 = "";
for (let i = name1.length - 1; i >= 0; i--) {
response1 += name1[i];
}
console.log(response1);
}
function iteration33(name1, name2) {
console.log("Iteration 3.3");
let hacker1 = name1;
let hacker2 = name2;
let minlength = 0;
if (hacker1.length < hacker2.length) {
minlength = hacker1.length;
} else {
minlength = hacker2.length;
}
for (let index = 0; index < minlength; index++) {
if (hacker1 === hacker2) {
console.log("What?! You both have the same name?");
break;
}
if (hacker1[index] < hacker2[index]) {
console.log("The driver's name goes first.");
break;
} else if (hacker1[index] > hacker2[index]) {
console.log("Yo, the navigator goes first, definitely.");
break;
}
}
}
function bonus1(longtext) {
console.log("Bonus 1");
console.log("El número total de palabras es " + longtext.split(/\s+/).length);
let comprobacion = "";
let contador = 0;
for (let i = 0; i < longtext.length - 2; i++) {
comprobacion = longtext[i] + longtext[i + 1] + longtext[i + 2];
if (comprobacion == " et") {
contador++;
}
}
console.log(contador);
}
function bonus2(phraseToCheck) {
console.log("Bonus 2");
phraseToCheck = phraseToCheck.toLowerCase();
let text = phraseToCheck.replace(/[^a-zñ]/g, "");
let textInvert = "";
for (let i = text.length - 1; i >= 0; i--) {
textInvert += text[i];
}
if (text === textInvert) {
console.log("Es palíndomo");
} else {
console.log("No es palíndromo");
}
}
let name1 = "John";
let name2 = "Maria";
iteration1(name1, name2);
iteration2(name1, name2);
iteration31(name1, name2);
iteration32(name1, name2);
iteration33(name1, name2);
let longtext = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam elit arcu, elementum id rhoncus at, porttitor non quam. Maecenas aliquet lacus quis volutpat rutrum. Curabitur quis molestie mi. Donec sollicitudin finibus risus quis fermentum. Vivamus luctus efficitur cursus. Fusce vitae convallis arcu. Duis in leo lacus. Sed et arcu urna. Suspendisse condimentum nulla elit, vitae mollis odio feugiat quis. Sed blandit, tellus vitae ornare luctus, ligula ligula dictum lectus, vel pellentesque tellus lacus non metus.
Aliquam lacinia vestibulum ipsum, at congue ligula porttitor non. Vestibulum volutpat lectus id dolor faucibus accumsan. Nulla pretium, erat eu hendrerit convallis, tellus elit convallis elit, sed pharetra mauris nisi ut nunc. In blandit egestas scelerisque. Nunc et justo nec nibh viverra blandit non vel lectus. Donec sollicitudin malesuada ex in scelerisque. Nullam ut arcu a erat tincidunt condimentum. Suspendisse ut imperdiet tortor, quis vulputate arcu. Nulla tincidunt viverra dictum. Fusce in odio libero. Quisque at viverra dui.
Morbi porta id justo nec consequat. Pellentesque vitae dui ut neque tempus finibus vitae ut justo. Sed eget tellus quis sapien sodales consequat laoreet in diam. Proin vel ex dapibus, sagittis diam et, varius nunc. Maecenas non justo metus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Quisque ultricies, nisl in tempor elementum, augue dui ullamcorper orci, et feugiat magna lectus eu sapien. Proin auctor velit arcu, ac feugiat mauris sagittis ut. Nunc elementum nibh sit amet nunc blandit placerat. Ut risus orci, lacinia ac mi at, tristique bibendum neque. Praesent id lacus in nulla venenatis hendrerit eget et nisi. Sed quis hendrerit ex.`;
let phraseToCheck = "Amor, Roma";
bonus1(longtext);
bonus2(phraseToCheck);