Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

Using `setInterval`:
Usando `setInterval`:

```js run
function printNumbers(from, to) {
Expand All @@ -14,11 +14,11 @@ function printNumbers(from, to) {
}, 1000);
}

// usage:
// uso:
printNumbers(5, 10);
```

Using nested `setTimeout`:
Usando `setTimeout` aninhado:


```js run
Expand All @@ -34,13 +34,13 @@ function printNumbers(from, to) {
}, 1000);
}

// usage:
// uso:
printNumbers(5, 10);
```

Note that in both solutions, there is an initial delay before the first output. The function is called after `1000ms` the first time.
Observe que, nas duas soluções, há um atraso inicial antes da primeira exibição. A função é chamada pela primeira vez depois de `1000ms`.

If we also want the function to run immediately, then we can add an additional call on a separate line, like this:
Se também quisermos que a função rode imediatamente, podemos adicionar uma chamada extra numa linha separada, assim:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not introduced by this PR, but this file is touched: in the third solution below, go() (line 58) runs before let timerId = setInterval(go, 1000). When from == to (e.g. printNumbers(5, 5)), go() reaches clearInterval(timerId) while timerId is still in the TDZ, so it throws ReferenceError: Cannot access timerId before initialization. Consider declaring let timerId; before calling go(), or report it upstream.


```js run
function printNumbers(from, to) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Output every second
# Exiba a cada segundo

Write a function `printNumbers(from, to)` that outputs a number every second, starting from `from` and ending with `to`.
Escreva uma função `printNumbers(from, to)` que exiba um número a cada segundo, começando de `from` e terminando em `to`.

Make two variants of the solution.
Faça duas variantes da solução.

1. Using `setInterval`.
2. Using nested `setTimeout`.
1. Usando `setInterval`.
2. Usando `setTimeout` aninhado.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@

Any `setTimeout` will run only after the current code has finished.
Qualquer `setTimeout` só vai rodar depois que o código atual tiver terminado.

The `i` will be the last one: `100000000`.
O `i` vai ser o último valor: `100000000`.

```js run
let i = 0;

setTimeout(() => alert(i), 100); // 100000000

// assume that the time to execute this function is >100ms
// assuma que o tempo para executar essa função é >100ms

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as task.md:23. This is a loop, not a function.

Suggested change
// assuma que o tempo para executar essa função é >100ms
// assuma que o tempo para executar este laço é >100ms

for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ importance: 5

---

# What will setTimeout show?
# O que o setTimeout vai mostrar?

In the code below there's a `setTimeout` call scheduled, then a heavy calculation is run, that takes more than 100ms to finish.
No código abaixo, há uma chamada `setTimeout` agendada, depois um cálculo pesado é executado, que leva mais de 100ms para terminar.

When will the scheduled function run?
Quando a função agendada vai rodar?

1. After the loop.
2. Before the loop.
3. In the beginning of the loop.
1. Depois do laço.
2. Antes do laço.
3. No início do laço.


What is `alert` going to show?
O que o `alert` vai mostrar?

```js
let i = 0;

setTimeout(() => alert(i), 100); // ?

// assume that the time to execute this function is >100ms
// assuma que o tempo para executar essa função é >100ms

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says "essa função", but what follows is a for loop, not a function. The English original has the same slip ("this function"); "este laço" is clearer and matches the task text ("Depois do laço"). The same comment appears in solution.md:11.

Suggested change
// assuma que o tempo para executar essa função é >100ms
// assuma que o tempo para executar este laço é >100ms

for(let j = 0; j < 100000000; j++) {
i++;
}
Expand Down
Loading