Skip to content

Strings #197

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 6 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('Transforma o 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(""), "");
});
});
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,21 +1,21 @@
We can't "replace" the first character, because strings in JavaScript are immutable.
Não podemos "substituir" o primeiro caractere, 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 caractere 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]` será `undefined`, e como `undefined` não possui o método `toUpperCase()`, recebemos um erro.

There are two variants here:
Existem aqui duas variantes:

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* (ainda que vazia).
2. Adicione um teste por uma *string* vazia.

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

```js run
```js run demo
function ucFirst(str) {
if (!str) return str;

Expand Down
4 changes: 2 additions & 2 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,9 +2,9 @@ importance: 5

---

# Uppercast the first character
# Transforme o primeiro caractere 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 caractere em maiúsculas, por exemplo:

```js
ucFirst("john") == "John";
Expand Down
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('nenhum spam em "coelhinha inocente"', function() {
assert.isFalse(checkSpam('coelhinha inocente'));
});
});
8 changes: 4 additions & 4 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 fazer a pesquisa insensível ao caso (*case-insensitive*), vamos transformar a *string* em minúsculas e a seguir pesquisar:

```js run
function checkSpam(str) {
Expand All @@ -7,8 +7,8 @@ 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("coelhinha inocente") );
```

12 changes: 6 additions & 6 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,15 +2,15 @@ importance: 5

---

# Check for spam
# Procure 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 em caso contrário retorne `false`.

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

```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("coelhinha 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 longa para o comprimento dado (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
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.
O comprimento máximo deve ser `maxlength`, portanto precisamos de a cortar um pouco antes, para dar espaço às reticências.

Note that there is actually a single Unicode character for an ellipsis. That's not three dots.
Note que realmente existe um caractere *unicode* único para as reticências. Não são três pontos.

```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
# 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 caractere 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
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
# Extraia o valor monetário

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 a seguir 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 faça a extração do valor numérico dessa *string* e o retorne.

The example:
O exemplo:

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