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
115 lines (66 loc) · 2.67 KB
/
index.js
File metadata and controls
115 lines (66 loc) · 2.67 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
// Iteration 1: Names and Input
let hacker1 = "Matias Driver";
let hacker2 = "Matias Navegador";
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
//1-pasar a mayuscula y con espacio
let nombreCompleto = "";
for(i = 0; i < hacker1.length; i++){
nombreCompleto += hacker1[i].toUpperCase();
nombreCompleto += " ";
}
console.log(nombreCompleto);
//2- Invertir nombre (opcion 1)
let resultadoInvertir1= "";
for (let i=hacker1.length - 1; i >= 0; i--) {
let letra = hacker1[i];
resultadoInvertir1 += letra;
}
console.log(resultadoInvertir1.toUpperCase());
//********** opcion 2
function invertir(nombre){
let separarNombre = nombre.split("");
let invertirNombre = separarNombre.reverse();
let nombreInvertido = invertirNombre.join("");
return nombreInvertido;
}
console.log(invertir(nombreCompleto));
//3- Orden lexicografico
function ordenLexicografico(cadena1,cadena2){
orden = cadena1.localeCompare(cadena2);
if(orden < 0){
console.log("The driver's name goes first.");
} else if (orden > 0){
console.log("Yo, the navigator goes first, definitely.");
} else{
console.log("What?! You both have the same name?");
}
}
ordenLexicografico(hacker1,hacker2);
//BONUS
let longText = "Lorem ipsum dolor sit amet consectetur adipisicing elit. Odio quos asperiores non hic sapiente, ea iste saepe nisi laborum eum quod. Distinctio consectetur commodi beatae, non hic esse nisi minima! Lorem ipsum dolor sit amet consectetur adipisicing elit. Fugit ab sunt delectus harum error veritatis, commodi, beatae quam quisquam veniam distinctio necessitatibus repellendus corporis aperiam, ad perspiciatis totam sit molestiae! Lorem ipsum dolor sit, amet consectetur adipisicing elit. Delectus necessitatibus eaque numquam veritatis voluptatem at magnam corrupti, veniam distinctio, consectetur optio aliquid facilis unde laboriosam? Ex a provident repellat voluptatum.";
function wordsCounter(frase){
words = frase.split(" ").length;
return words;
}
console.log(wordsCounter(longText));
if(longText.includes(" et ")){
console.log("hay una palabra que buscas")
} else{
console.log("no esta la palabra que buscas");
}