Skip to content

Strings #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Dec 18, 2021
6 changes: 3 additions & 3 deletions 1-js/05-data-types/03-string/1-ucfirst/_js.view/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
describe("ucFirst", function() {
it('Uppercases the first symbol', function() {
it('Перетворює перший символ у верхній регістр', function() {
assert.strictEqual(ucFirst("john"), "John");
});

it("Doesn't die on an empty string", function() {
it('Не вмирає на порожніх рядках', function() {
assert.strictEqual(ucFirst(""), "");
});
});
});
16 changes: 8 additions & 8 deletions 1-js/05-data-types/03-string/1-ucfirst/solution.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
We can't "replace" the first character, because strings in JavaScript are immutable.
Ми не можемо "замінити" перший символ, оскільки рядки в JavaScript незмінні.

But we can make a new string based on the existing one, with the uppercased first character:
Але ми можемо створити новий рядок на основі існуючого, з першим символом у верхньому регістрі:

```js
let newStr = str[0].toUpperCase() + str.slice(1);
```

There's a small problem though. If `str` is empty, then `str[0]` is `undefined`, and as `undefined` doesn't have the `toUpperCase()` method, we'll get an error.
Але є невелика проблема. Якщо `str` порожній рядок, то `str[0]` буде `undefined`, а оскільки `undefined` не має методу `toUpperCase()`, ми отримаємо помилку.

There are two variants here:
Тут є два варіанти:

1. Use `str.charAt(0)`, as it always returns a string (maybe empty).
2. Add a test for an empty string.
1. Використати `str.charAt(0)`, оскільки він завжди повертає рядок (навіть для порожнього рядка).
2. Додати перевірку на порожній рядок.

Here's the 2nd variant:
Ось 2-й варіант:

```js run demo
function ucFirst(str) {
Expand All @@ -22,6 +22,6 @@ function ucFirst(str) {
return str[0].toUpperCase() + str.slice(1);
}

alert( ucFirst("john") ); // John
alert( ucFirst("василь") ); // Василь
```

6 changes: 3 additions & 3 deletions 1-js/05-data-types/03-string/1-ucfirst/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Uppercase the first character
# Переведіть перший символ у верхній регістр

Write a function `ucFirst(str)` that returns the string `str` with the uppercased first character, for instance:
Напишіть функцію `ucFirst(str)`, яка повертає рядок `str` з першим символом у верхньому регістрі, наприклад:

```js
ucFirst("john") == "John";
ucFirst("василь") == "Василь";
```

8 changes: 4 additions & 4 deletions 1-js/05-data-types/03-string/2-check-spam/_js.view/test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
describe("checkSpam", function() {
it('finds spam in "buy ViAgRA now"', function() {
it('знаходить спам у "buy ViAgRA now"', function() {
assert.isTrue(checkSpam('buy ViAgRA now'));
});

it('finds spam in "free xxxxx"', function() {
it('знаходить спам у "free xxxxx"', function() {
assert.isTrue(checkSpam('free xxxxx'));
});

it('no spam in "innocent rabbit"', function() {
it('не знаходить спам у "innocent rabbit"', function() {
assert.isFalse(checkSpam('innocent rabbit'));
});
});
});
2 changes: 1 addition & 1 deletion 1-js/05-data-types/03-string/2-check-spam/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
To make the search case-insensitive, let's bring the string to lower case and then search:
Щоб зробити пошук нечутливим до регістру, давайте переведемо рядок у нижній регістр, а потім здійснимо пошук:

```js run demo
function checkSpam(str) {
Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/03-string/2-check-spam/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Check for spam
# Перевірка на спам

Write a function `checkSpam(str)` that returns `true` if `str` contains 'viagra' or 'XXX', otherwise `false`.
Напишіть функцію `checkSpam(str)`, яка повертає `true`, якщо `str` містить 'viagra' or 'XXX', інакше `false`.

The function must be case-insensitive:
Функція має бути нечутливою до регістру:

```js
checkSpam('buy ViAgRA now') == true
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/03-string/3-truncate/_js.view/test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
describe("truncate", function() {
it("truncate the long string to the given length (including the ellipsis)", function() {
it("урізає довгий рядок до заданої довжини (включаючи три крапки)", function() {
assert.equal(
truncate("What I'd like to tell on this topic is:", 20),
"What I'd like to te…"
truncate("Що я хотів би розповісти на цю тему:", 20),
"Що я хотів би розпо…"
);
});

it("doesn't change short strings", function() {
it("не змінює короткі рядки", function() {
assert.equal(
truncate("Hi everyone!", 20),
"Hi everyone!"
truncate("Всім привіт!", 20),
"Всім привіт!"
);
});

Expand Down
4 changes: 2 additions & 2 deletions 1-js/05-data-types/03-string/3-truncate/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis.
Максимальна довжина має бути `maxlength`, тому нам потрібно її трохи обрізати, щоб дати місце для символу трьох крапок.

Note that there is actually a single Unicode character for an ellipsis. That's not three dots.
Зауважте, що насправді існує один юнікодний символ для "трьох крапок". Це не три послідовні крапки.

```js run demo
function truncate(str, maxlength) {
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/03-string/3-truncate/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ importance: 5

---

# Truncate the text
# Урізання тексту

Create a function `truncate(str, maxlength)` that checks the length of the `str` and, if it exceeds `maxlength` -- replaces the end of `str` with the ellipsis character `"…"`, to make its length equal to `maxlength`.
Створіть функцію `truncate(str, maxlength)`, яка перевіряє довжину `str` і, якщо вона перевищує `maxlength` -- замінює кінець `str` символом трьох крапок `"…"`, щоб його довжина була рівною `maxlength`.

The result of the function should be the truncated (if needed) string.
Результатом функції повинен бути урізаний (якщо потребується) рядок.

For instance:
Наприклад:

```js
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
truncate("Що я хотів би розповісти на цю тему:", 20) = "Що я хотів би розпо…"

truncate("Hi everyone!", 20) = "Hi everyone!"
truncate("Всім привіт!", 20) = "Всім привіт!"
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
describe("extractCurrencyValue", function() {

it("for the string $120 returns the number 120", function() {
it("для рядка $120 повертає число 120", function() {
assert.strictEqual(extractCurrencyValue('$120'), 120);
});


});
});
8 changes: 4 additions & 4 deletions 1-js/05-data-types/03-string/4-extract-currency/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 4

---

# Extract the money
# Виділіть гроші

We have a cost in the form `"$120"`. That is: the dollar sign goes first, and then the number.
У нас є вартість у вигляді `"$120"`. Тобто: спочатку йде знак долара, а потім число.

Create a function `extractCurrencyValue(str)` that would extract the numeric value from such string and return it.
Створіть функцію `extractCurrencyValue(str)`, яка витягне числове значення з такого рядка та поверне його.

The example:
Приклад:

```js
alert( extractCurrencyValue('$120') === 120 ); // true
Expand Down
Loading