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
118 lines (91 loc) · 5.55 KB
/
index.js
File metadata and controls
118 lines (91 loc) · 5.55 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
//1.1 Create a variable hacker1 with the driver's name.
const driver = prompt('Escribe el nombre del Driver')
//1.2 Print "The driver's name is XXXX".
console.log(`The driver's name is ${driver}`)
/*1.3 Create a variable hacker2 with the navigator's name.
1.4 Print "The navigator's name is YYYY".*/
const navigator = prompt('Escribe el nombre del Navigator')
console.log(`The navigator's name is ${navigator}`)
/*2.1. Depending on which name is longer, print:
- The driver has the longest name, it has XX characters. or
- It seems that the navigator has the longest name, it has XX characters. or
- Wow, you both have equally long names, XX characters!.*/
if (driver.length > navigator.length){
console.log(`The driver has the longest name, it has ${driver.length} characters`)
}else if(driver.length < navigator.length){
console.log(`It seems that the navigator has the longest name, it has ${navigator.length} characters`)
}/*else if(driver.length === navigator.length){
console.log(`WOW! you both have equally long names, navigator have ${navigator.length} characters and driver have ${driver.length} characters`)}*/
else if(driver.length === navigator.length){
console.log(`WOW! you both have equally long names, navigator have ${navigator.length} characters and driver have ${driver.length} characters`)}
//3.1 Print the characters of the driver's name, separated by space, and in capital letters, i.e., "J O H N".
let result = ''
for(let i=0; i < (driver.length); i++){
const currentLetter = driver[i]
if(i < driver.length-1){
result += `${currentLetter} `
}else {
result += currentLetter
}
}
console.log(result.toUpperCase())
//3.2 Print all the characters of the navigator's name in reverse order, i.e., "nhoJ".
for (let i = navigator.length-1; i >= 0; i--){
console.log(navigator[i])
}
/*3.3 Depending on the lexicographic order of the strings, print:
The driver's name goes first.
Yo, the navigator goes first, definitely.
What?! You both have the same name?
'str1'.localeCompare('str2');*/
console.log(driver,navigator)
if (driver.localeCompare(navigator) === -1){
console.log("The driver's name goes first.");
}else if (navigator.localeCompare(driver) === -1){
console.log("Yo, the navigator goes first, definitely.");
}else{
console.log('What?! You both have the same name?')
}
/* Bonus 1:
Go to the lorem ipsum generator website and:
Generate 3 paragraphs. Store the text in a new string variable named longText.
Make your program count the number of words in the string.
Make your program count the number of times the Latin word et appears. */
const longText = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam viverra porttitor urna, ac tincidunt arcu ornare eget. Etiam risus risus, porta sit amet ullamcorper eget, congue vel urna. Nunc sapien quam, sodales non fermentum at, venenatis et eros. Pellentesque pulvinar dolor a convallis ultricies. Nam in placerat tortor. Nunc varius ut nisl ut commodo. Aliquam auctor ac erat nec tincidunt. Donec arcu felis, hendrerit in metus eu, porta facilisis ipsum. Sed facilisis diam leo, eu porttitor magna lacinia ut. Mauris sit amet dictum ante. Pellentesque viverra massa eget molestie faucibus. Etiam nec molestie mi. Pellentesque a gravida risus. Ut sed arcu at lorem accumsan accumsan a nec lacus. In hac habitasse platea dictumst. Praesent at convallis libero, id ultricies libero. \nDuis hendrerit rhoncus sollicitudin. In hac habitasse platea dictumst. Sed tempus nulla nec sapien sagittis auctor. Fusce rutrum mauris ac pretium elementum. Quisque ultrices auctor nisi nec ornare. Donec magna lacus, tempus vitae aliquam et, ultricies sed urna. Sed interdum imperdiet nisl et ullamcorper. Nullam convallis et sem ac ultrices. \nDonec imperdiet hendrerit arcu. Mauris commodo vehicula est, eu malesuada urna rutrum eget. Proin eget elit vestibulum, aliquam ipsum quis, vehicula dolor. Donec sed justo in nisi condimentum congue a eget nisl. Proin viverra vulputate tortor in fermentum. Sed urna elit, consequat ut vehicula consectetur, sodales non ante. Nullam eu neque quis lectus cursus fringilla.'
//OPCION 1
let dividedText = longText.split(" ")
let numberWords = dividedText.length
let latinWord = longText.split(" et ")
let numberLatinWords = latinWord.length
/*for (let i = 0; i < longText.length; i++){
const twoChar = longText[i] + longText[i+1];
if (twoChar === "et"){
latinWord++
}
}*/
console.log(`This text have ${numberWords}, and "et" appears ${numberLatinWords} times`); //funciona usando comando split, pero no con metodos de clase
//OPCION 2
longText = longText.replace(/(^\s*)/g,""); // la primera barra significa expresion regular y la barra g es para que busque en todo el texto
console.log(`This text have ${longText.split(' ').length} words`)
let latinWord = 0
for (let i =0; i < longText.length; i++){
if (longText[i] === 'e' && longText[i+1] === 't' && longText[i-1] === ' '){
latinWord++
}
}
console.log(`In this text appears "et" ${latinWord} times.`)
/*Create a new variable, phraseToCheck, containing some string value. Write a code to check if the value assigned to this variable is a Palindrome. Here are some examples of palindromes:*/
const phraseToCheck = prompt('Escribe la frase')
let result = ''
let resultReverse = ''
for (let i = 0; i < phraseToCheck.length; i++){
result = phraseToCheck[i].toLowerCase();
}
for (let i = phraseToCheck.length -1; i >=0; i--){
resultReverse = phraseToCheck[i].toLowerCase();
}
if (result === resultReverse){
console.log(`${phraseToCheck} is a Palindrome`)
}else {
console.log(`'${phraseToCheck}' is not a Palindrome`)
}