You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/05-array-methods/6-calculator-extendable/task.md
+15-12Lines changed: 15 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,35 +2,38 @@ importance: 5
2
2
3
3
---
4
4
5
-
# Create an extendable calculator
5
+
# Crie uma calculadora extensível
6
6
7
-
Create a constructor function`Calculator`that creates "extendable" calculator objects.
7
+
Construa uma função construtora`Calculator`que crie objetos de calculadora "extensível".
8
8
9
-
The task consists of two parts.
9
+
A atividade consiste de duas partes.
10
10
11
-
1.First, implement the method`calculate(str)`that takes a string like`"1 + 2"`in the format "NUMBER operator NUMBER" (space-delimited) and returns the result. Should understand plus `+`and minus`-`.
11
+
1.Primeiro, implemente o método`calculate(str)`que pegue uma string como`"1 + 2"`e a deixe no formato "NÚMERO operador NÚMERO" (delimitada por espaços) e retorne o resultado. Deve entender os operadores mais `+`e menos`-`.
12
12
13
-
Usage example:
13
+
Exemplo:
14
14
15
15
```js
16
16
let calc =newCalculator;
17
17
18
18
alert( calc.calculate("3 + 7") ); // 10
19
19
```
20
-
2.Then add the method`addMethod(name, func)`that teaches the calculator a newoperation. It takes the operator `name`and the two-argument function`func(a,b)` that implements it.
20
+
2.Então, adicione o método`addMethod(name, func)`que ensina a calculadora uma nova operação. O método pega o operador `name`e a função que recebe dois argumentos `func(a,b)`para implementar a nova operação.
21
21
22
-
For instance, let's add the multiplication `*`, division `/` and power `**`:
22
+
Por exemplo, vamos adicionar multiplicação `*`, divisão`/`e potenciação`**`:
23
23
24
24
```js
25
+
let multCalc = new Calculator;
26
+
let divCalc = new Calculator;
25
27
let powerCalc = new Calculator;
26
-
powerCalc.addMethod("*", (a, b) => a * b);
27
-
powerCalc.addMethod("/", (a, b) => a / b);
28
+
29
+
multCalc.addMethod("*", (a, b) => a * b);
30
+
divCalc.addMethod("/", (a, b) => a / b);
28
31
powerCalc.addMethod("**", (a, b) => a ** b);
29
32
30
33
let result = powerCalc.calculate("2 ** 3");
31
34
alert( result ); // 8
32
35
```
33
36
34
-
- No brackets or complex expressions in this task.
35
-
- The numbers and the operator are delimited with exactly one space.
36
-
- There may be error handling if you'd like to add it.
37
+
-Sem colchetes ou expressões complexas neste exercício.
38
+
-Os números e o operador são separados por, exatamente, um espaço.
39
+
-Pode haver mensagem de erro se você desejar adicionar.
0 commit comments