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
89 lines (61 loc) · 3.69 KB
/
index.js
File metadata and controls
89 lines (61 loc) · 3.69 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
// Iteration 1: Names and Input
let hacker1 = "Brandon";
let hacker2 = "Alejandra"
console.log(`The driver's name is ${hacker1}`)
console.log(`The navigator's name is ${hacker2}`)
// Iteration 2: Conditionals
let hacker1 = "Brandon";
let hacker2 ="Alejandra"
console.log(`The driver's name is ${hacker1}`)
console.log(`The navigator's name is ${hacker2}`)
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, ${hacker1.length} characters!`)
}
// Iteration 3: Loops
// Iteration 3.1 CAPS separated by space
let hacker1 = "Brandon"
let hacker2 = "Alejandra"
hacker1 = hacker1.toUpperCase();
const lastIndex = hacker1.length - 1;
let newName = "";
for (let i = 0; i <= lastIndex; i++) {
newName = newName + " " + hacker1[i];
}
console.log(newName);
// Iteration 3.2 Reverse order
let hacker2Reversed = "";
for (let i = hacker2.length-1; i>=0; i--) {
hacker2Reversed = hacker2Reversed + hacker2[i]
}
console.log(hacker2Reversed);
// Iteration 3.3 Lexicographic order
let result = hacker1.localeCompare(hacker2);
if (result < 0) {
console.log("The driver's name goes first.");
} else if (result > 0) {
console.log("Yo, the navigator goes first definitely.");
}
else {
console.log("What?! You both have the same name?");
}
// Bonus 1:
// Generate 3 paragraphs. Store the text in a new string variable named longText.
let longText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sem odio, congue et tortor id, imperdiet bibendum dolor. Etiam ut imperdiet ex. Sed lacinia ullamcorper arcu, non fringilla mauris tempus vitae. Vivamus consectetur justo ut condimentum rutrum. Ut vel urna id ante semper cursus. Quisque eu eleifend ligula. Nunc tortor diam, rutrum in mattis id, tristique euismod ligula. Maecenas viverra ex ac viverra venenatis.
Nullam feugiat leo quis accumsan sagittis. In malesuada rhoncus justo. Aliquam id massa ante. Fusce maximus gravida purus, at vestibulum magna feugiat eget. Aliquam interdum odio sit amet lectus scelerisque, nec pulvinar ex convallis. Nam vel purus magna. Etiam vitae nisl ante. Aenean a lorem ut libero aliquam semper ultrices sed sem. Proin mollis eros quam, id tempus sapien bibendum id. In ultrices, tortor sed cursus sagittis, lorem erat vestibulum urna, in dapibus massa tellus at metus. Aliquam ac metus ligula. Quisque luctus urna metus, vel elementum lorem iaculis eu. Proin sed turpis condimentum, sodales nulla in, semper nulla. Nullam vitae tempor velit.
Cras elit erat, mattis auctor odio at, fermentum tincidunt massa. Nam faucibus gravida eros vitae maximus. Cras odio massa, blandit at ornare vitae, sollicitudin non nibh. Duis vel interdum nunc. Suspendisse tristique at justo vel mollis. Curabitur vestibulum convallis lobortis. Fusce nibh erat, tempor et lacus a, sodales sollicitudin orci. Aliquam mattis accumsan maximus. Nullam nunc nunc, condimentum nec enim pellentesque, auctor lacinia orci. Nunc justo sapien, posuere eu dictum egestas, gravida vitae turpis. In tempus tempor orci at viverra. Pellentesque et volutpat sem, vitae ultricies ante. Aenean finibus hendrerit rhoncus.`
// Make your program count the number of words in the string.
const myArray = longText.split(" ");
console.log(myArray.length);
// Count the number of times the Latin word et appears.
let count = 0;
for (let i = 0; i < longText.length; i++) {
const twoChars = longText[i] + longText[i + 1];
if ( twoChars === "et" ) {
count++;
}
}
console.log(count);