Skip to content

Commit

Permalink
exercício 02- cap.03 funções #9
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdouglas committed Dec 1, 2014
1 parent 5541123 commit 042dc4c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions chapters/03-funcoes.md
Original file line number Diff line number Diff line change
Expand Up @@ -551,37 +551,37 @@ Separando as tarefas diferentes seu programa executa diferentes funções e isso

### Mínimo

The previous chapter introduced the standard function Math.min that returns its smallest argument. We can do that ourselves now. Write a function min that takes two arguments and returns their minimum.
O capítulo anterior introduziu a função padrão `Math.min` que retorna seu menor argumento. Nós podemos fazer isso nós mesmos agora. Escreva uma função `min` que recebe dois argumentos e retorna o valor mínimo.

```js
// Your code here.
// Seu código aqui.

console.log(min(0, 10));
// → 0
console.log(min(0, -10));
// → -10
```

T> If you have trouble putting braces and parentheses in the right place to get a valid function definition, start by copying one of the examples in this chapter and modifying it.
T> Se você tiver problemas para colocar as chaves e parênteses no lugar certo para ter uma definição de função válida, comece copiando um dos exemplos neste capítulo e modifique-o.
T>
T> A function may contain multiple return statements.
T> Uma função pode conter múltiplas declarações `return`.

### Recursion
### Recursão

We’ve seen that % (the remainder operator) can be used to test whether a number is even or odd by using % 2 to check whether it’s divisible by two. Here’s another way to define whether a positive whole number is even or odd:
Vimos que o `%` (operador resto) pode ser usado para testar se um número é par ou ímpar usando `% 2` para verificar se o número é divisível por dois. Aqui uma outra maneira de verificar se um número inteiro positivo é par ou ímpar:

* Zero is even.
* Zero é par.

* One is odd.
* Um é ímpar.

* For any other number N, its evenness is the same as N - 2.
* Para qualquer outro número N, seu padrão é o mesmo que N - 2.

Define a recursive function isEven corresponding to this description. The function should accept a number parameter and return a Boolean.
Defina uma função recursiva `isEven` correspondente a essa descrição. A função deve aceitar um número como parâmetro e retornar um `Boolean`.

Test it on 50 and 75. See how it behaves on -1. Why? Can you think of a way to fix this?
Teste isso com 50 e 75. Veja como se comporta com -1. Por que? Você pode pensar numa forma de consertar isso?

```js
// Your code here.
// Seu código aqui.

console.log(isEven(50));
// → true
Expand All @@ -591,9 +591,9 @@ console.log(isEven(-1));
// → ??
```

T> Your function will likely look somewhat similar to the inner `find` function in the recursive `findSolution` example in this chapter, with an `if/else if/else` chain that tests which of the three cases applies. The final else, corresponding to the third case, makes the recursive call. Each of the branches should contain a return statement or in some other way arrange for a specific value to be returned.
T> Sua função vai parecer com a função `find` no exemplo recursivo `findSolution` neste capítulo, com uma cadeia `if/else if/else` que testa qual dos três casos será aplicado. Cada um dos *branches* (ramificações) deverá conter uma declaração `return` ou ser organizada de outra forma para um valor específica para ser retornado.
T>
T> When given a negative number, the function will recurse again and again, passing itself an ever more negative number, thus getting further and further away from returning a result. It will eventually run out of stack space and abort.
T> Quando for passado um número negativo, a função vai ser chamada de novo e de novo, passando para si mesma um número cada vez mais negativo, indo sempre mais longe de retornar um resultado. Isso eventualmente vai sair do espaço da memória e abortar.

### Bean counting

Expand All @@ -614,4 +614,4 @@ console.log(countChar("kakkerlak", "k"));

T> A loop in your function will have to look at every character in the string by running an index from zero to one below its length (< string.length). If the character at the current position is the same as the one the function is looking for, it adds 1 to a counter variable. Once the loop has finished, the counter can be returned.
T>
T> Take care to make all the variables used in the function local to the function by using the var keyword.
T> Take care to make all the variables used in the function local to the function by using the var keyword.

1 comment on commit 042dc4c

@joaostein
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.