JavaScript: методи рядків та Math

Методи рядків

📖 Теорія

Рядки — незмінні (immutable). Всі методи повертають новий рядок.

JavaScript
1class="hl-keyword">const s = class="hl-string">'Hello, World!';
2
3s.length          class="hl-comment">// 13
4s.toUpperCase()   class="hl-comment">// class="hl-string">'HELLO, WORLD!'
5s.toLowerCase()   class="hl-comment">// class="hl-string">'hello, world!'
6s.trim()          class="hl-comment">// прибирає пробіли з країв
7s.trimStart()     class="hl-comment">// тільки зліва
8s.trimEnd()       class="hl-comment">// тільки справа
9
10s.includes(class="hl-string">'World')    class="hl-comment">// true
11s.startsWith(class="hl-string">'Hello')  class="hl-comment">// true
12s.endsWith(class="hl-string">'!')        class="hl-comment">// true
13s.indexOf(class="hl-string">'o')         class="hl-comment">// 4 (перше входження)
14s.lastIndexOf(class="hl-string">'o')     class="hl-comment">// 8
15
16s.slice(7, 12)         class="hl-comment">// class="hl-string">'World'
17s.slice(-6)            class="hl-comment">// class="hl-string">'orld!'
18s.substring(0, 5)      class="hl-comment">// class="hl-string">'Hello'
19s.replace(class="hl-string">'World', class="hl-string">'JS') class="hl-comment">// class="hl-string">'Hello, JS!'
20s.replaceAll(class="hl-string">'l', class="hl-string">'L')   class="hl-comment">// class="hl-string">'HeLLo, WorLd!'
21s.split(class="hl-string">', ')          class="hl-comment">// [class="hl-string">'Hello', class="hl-string">'World!']
22s.repeat(2)            class="hl-comment">// class="hl-string">'Hello, World!Hello, World!'

'42'.padStart(5, '0') // '00042'
'hi'.padEnd(5, '.') // 'hi...'

📝 ЗАВДАННЯ (3)
1.
Завдання 1: Базові методи рядків
10 XP
Маємо рядок s = ' JavaScript '. Виведи:
1. Рядок без пробілів з країв
2. Його довжину після trim
3. Рядок великими літерами після trim
💡 Підказка: Застосуй .trim() спочатку, потім інші методи
🔓 Розв'язок:
const s = '  JavaScript  ';
const trimmed = s.trim();
console.log(trimmed);
console.log(trimmed.length);
console.log(trimmed.toUpperCase());
Вивід:

                                

2.
Завдання 2: Пошук та заміна
20 XP
Маємо рядок email = 'user@Example.COM'. Виведи:
1. email у нижньому регістрі
2. Чи містить email символ '@'
3. Замін 'Example' на 'gmail' у нижньому регістрі
💡 Підказка: Застосуй toLowerCase(), includes(), replace()
🔓 Розв'язок:
const email = 'user@Example.COM';
console.log(email.toLowerCase());
console.log(email.includes('@'));
console.log(email.toLowerCase().replace('example', 'gmail'));
Вивід:

                                

3.
Завдання 3: Парсинг рядка
30 XP
Маємо рядок data = 'Іван,25,Київ,розробник'. Виведи кожне поле на окремому рядку з поясненням:
Ім'я: Іван
Вік: 25
Місто: Київ
Професія: розробник
💡 Підказка: Використай split(',') щоб розбити рядок на масив
🔓 Розв'язок:
const data = 'Іван,25,Київ,розробник';
const parts = data.split(',');
console.log(`Ім'я: ${parts[0]}`);
console.log(`Вік: ${parts[1]}`);
console.log(`Місто: ${parts[2]}`);
console.log(`Професія: ${parts[3]}`);
Вивід: