Skip to content

Commit 38ecb42

Browse files
#4 - Javascript
1 parent d475c85 commit 38ecb42

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Acceso a caracteres específicos
2+
let string = 'Hello World!';
3+
let char = string[1]; // 'e'
4+
5+
// Subcadenas
6+
let substring = string.substring(0, 5); // 'Hello'
7+
8+
// Longitud
9+
let length = string.length; // 12
10+
11+
// Concatenación
12+
let concatenated = string + ' How are you?'; // 'Hello World! How are you?'
13+
14+
// Repetición
15+
let repeated = string.repeat(3); // 'Hello World!Hello World!Hello World!'
16+
17+
// Recorrido
18+
for (let i = 0; i < string.length; i++) {
19+
console.log(string[i]);
20+
}
21+
22+
// Conversión a mayúsculas
23+
let upper = string.toUpperCase(); // 'HELLO WORLD!'
24+
25+
// Conversión a minúsculas
26+
let lower = string.toLowerCase(); // 'hello world!'
27+
28+
// Reemplazo
29+
let replaced = string.replace('World', 'Everyone'); // 'Hello Everyone!'
30+
31+
// División
32+
let split = string.split(' '); // ['Hello', 'World!']
33+
34+
// Unión
35+
let joined = split.join(', '); // 'Hello, World!'
36+
37+
// Interpolación
38+
let name = 'Alice';
39+
let interpolated = `Hello, ${name}!`; // 'Hello, Alice!'
40+
41+
// Verificación (includes)
42+
let contains = string.includes('World'); // true
43+
44+
45+
46+
// Ejercicio:
47+
function isPalindrome(word) {
48+
return word === word.split('').reverse().join('');
49+
}
50+
51+
function isAnagram(word1, word2) {
52+
let normalize = (str) => str.toLowerCase().split('').sort().join('');
53+
return normalize(word1) === normalize(word2);
54+
}
55+
56+
function isIsogram(word) {
57+
let letters = word.toLowerCase().split('').sort();
58+
for (let i = 1; i < letters.length; i++) {
59+
if (letters[i] === letters[i - 1]) {
60+
return false;
61+
}
62+
}
63+
return true;
64+
}
65+
66+
// Use the functions
67+
let word1 = 'listen';
68+
let word2 = 'silent';
69+
70+
console.log(`Is "${word1}" a palindrome?`, isPalindrome(word1));
71+
console.log(`Is "${word2}" a palindrome?`, isPalindrome(word2));
72+
console.log(`Are "${word1}" and "${word2}" anagrams?`, isAnagram(word1, word2));
73+
console.log(`Is "${word1}" an isogram?`, isIsogram(word1));
74+
console.log(`Is "${word2}" an isogram?`, isIsogram(word2));

0 commit comments

Comments
 (0)