Skip to content

Strings #94

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 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('Primeiro símbolo em maiúsculas', function() {
assert.strictEqual(ucFirst("john"), "John");
});

it("Doesn't die on an empty string", function() {
it("Não aborta numa string vazia", function() {
assert.strictEqual(ucFirst(""), "");
});
});
15 changes: 7 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.
Não podemos "substituir" o primeiro carátere, porque *strings* em JavaScript são imutáveis.

But we can make a new string based on the existing one, with the uppercased first character:
Mas podemos criar uma nova *string* com base na existente, com o primeiro carátere em maiúsculas:

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

There's a small problem though. If `str` is empty, then `str[0]` is undefined, so we'll get an error.
Contudo, há um pequeno problema. Se `str` estiver vazia, então `str[0]` é *undefined*, e assim obtemos um erro.

There are two variants here:
Existem duas variantes aqui:

1. Use `str.charAt(0)`, as it always returns a string (maybe empty).
2. Add a test for an empty string.
1. Use `str.charAt(0)`, porque sempre retorna uma *string* (por vezes vazia).
2. Adicione um teste por uma *string* vazia.

Here's the 2nd variant:
Aqui está a segunda variante:

```js run
function ucFirst(str) {
Expand All @@ -24,4 +24,3 @@ function ucFirst(str) {

alert( ucFirst("john") ); // John
```

7 changes: 3 additions & 4 deletions 1-js/05-data-types/03-string/1-ucfirst/task.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
importance: 5
importância: 5

---

# Uppercast the first character
# Primeiro carátere em maiúsculas

Write a function `ucFirst(str)` that returns the string `str` with the uppercased first character, for instance:
Escreva uma função `ucFirst(str)` que retorne a *string* `str` com o primeiro carátere em maiúsculas, por exemplo:

```js
ucFirst("john") == "John";
```

12 changes: 6 additions & 6 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() {
assert.isTrue(checkSpam('buy ViAgRA now'));
it('encontra spam em "compre ViAgRA agora"', function() {
assert.isTrue(checkSpam('compre ViAgRA agora'));
});

it('finds spam in "free xxxxx"', function() {
assert.isTrue(checkSpam('free xxxxx'));
it('encontra spam em "xxxxx grátis"', function() {
assert.isTrue(checkSpam('xxxxx grátis'));
});

it('no spam in "innocent rabbit"', function() {
assert.isFalse(checkSpam('innocent rabbit'));
it('não encontra spam em "coelha inocente"', function() {
assert.isFalse(checkSpam('coelha inocente'));
});
});
9 changes: 4 additions & 5 deletions 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:
Para tornar a pesquisa não sensível a maiúsculas/minúsculas (*case-insensitive*), vamos transformar a *string* em minúsculas e depois efetuar a pesquisa:

```js run
function checkSpam(str) {
Expand All @@ -7,8 +7,7 @@ function checkSpam(str) {
return lowerStr.includes('viagra') || lowerStr.includes('xxx');
}

alert( checkSpam('buy ViAgRA now') );
alert( checkSpam('free xxxxx') );
alert( checkSpam("innocent rabbit") );
alert( checkSpam('compre ViAgRA agora') );
alert( checkSpam('xxxxx grátis') );
alert( checkSpam("coelha inocente") );
```

15 changes: 7 additions & 8 deletions 1-js/05-data-types/03-string/2-check-spam/task.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
importance: 5
importância: 5

---

# Check for spam
# Verifica por spam

Write a function `checkSpam(str)` that returns `true` if `str` contains 'viagra' or 'XXX', otherwise `false.
Escreva uma função `checkSpam(str)` que retorne `true` se `str` contiver 'viagra' ou 'XXX', e caso contrário retorne `false`.

The function must be case-insensitive:
A função não deve ser sensível a maiúsculas/minúsculas (*case-insensitive*):

```js
checkSpam('buy ViAgRA now') == true
checkSpam('free xxxxx') == true
checkSpam("innocent rabbit") == false
checkSpam('compre ViAgRA agora') == true
checkSpam('xxxxx grátis') == true
checkSpam("coelha inocente") == false
```

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("trunque a string comprida para o comprimento pretendido (incluindo as reticências)", function() {
assert.equal(
truncate("What I'd like to tell on this topic is:", 20),
"What I'd like to te…"
truncate("O que eu gostaria de dizer neste tópico é:", 20),
"O que eu gostaria d…"
);
});

it("doesn't change short strings", function() {
it("não altera strings curtas", function() {
assert.equal(
truncate("Hi everyone!", 20),
"Hi everyone!"
truncate("Olá a todos!", 20),
"Olá a todos!"
);
});

Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/03-string/3-truncate/solution.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
The maximal length must be `maxlength`, so we need to cut it a little shorter, to give space for the ellipsis.
O comprimento máximo deve ser `maxlength`, assim precisamos de a cortar um pouco antes, para dar espaço às reticências (*ellipsis*).

Note that there is actually a single unicode character for an ellipsis. That's not three dots.
Note que na verdade existe apenas um único carátere *unicode* para as reticências. Não são três pontos.

```js run demo
```js execute demo
function truncate(str, maxlength) {
return (str.length > maxlength) ?
str.slice(0, maxlength - 1) + '…' : str;
Expand Down
14 changes: 7 additions & 7 deletions 1-js/05-data-types/03-string/3-truncate/task.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
importance: 5
importância: 5

---

# Truncate the text
# Trunque o texto

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`.
Crie uma função `truncate(str, maxlength)` que verifique o comprimento de `str` e, se exceder a `maxlength` -- substitua o final de `str` pelo carátere reticências `"…"`, afim de tornar o seu comprimento igual a `maxlength`.

The result of the function should be the truncated (if needed) string.
O resultado da função deverá ser a *string* truncada (se necessário).

For instance:
Por exemplo:

```js
truncate("What I'd like to tell on this topic is:", 20) = "What I'd like to te…"
truncate("O que eu gostaria de dizer neste tópico é:", 20) = "O que eu gostaria d…"

truncate("Hi everyone!", 20) = "Hi everyone!"
truncate("Olá a todos!", 20) = "Olá a todos!"
```
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe("extractCurrencyValue", function() {

it("for the string $120 returns the number 120", function() {
it("para a string $120 retorne o número 120", function() {
assert.strictEqual(extractCurrencyValue('$120'), 120);
});

Expand Down
10 changes: 5 additions & 5 deletions 1-js/05-data-types/03-string/4-extract-currency/task.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
importance: 4
importância: 4

---

# Extract the money
# Extraia o dinheiro

We have a cost in the form `"$120"`. That is: the dollar sign goes first, and then the number.
Temos um custo na forma `"$120"`. Isto é: o sinal de dólar vem primeiro, e depois o número.

Create a function `extractCurrencyValue(str)` that would extract the numeric value from such string and return it.
Crie uma função `extractCurrencyValue(str)`, que irá extrair o valor numérico dessa *string* e o retornar.

The example:

```js
alert( extractCurrencyValue('$120') === 120 ); // true
alert( extractCurrencyValue('$120') === 120 ); // true (verdadeiro)
```

Loading