Skip to content

Coding Style #69

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 1 commit 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
33 changes: 16 additions & 17 deletions 1-js/03-code-quality/02-coding-style/1-style-errors/solution.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@

You could note the following:
Poderia notar no seguinte:

```js no-beautify
function pow(x,n) // <- no space between arguments
{ // <- figure bracket on a separate line
let result=1; // <- no spaces before or after =
for(let i=0;i<n;i++) {result*=x;} // <- no spaces
// the contents of { ... } should be on a new line
function pow(x,n) // <- nenhum espaço entre argumentos
{ // <- chaveta de abertura numa linha em separado
let result=1; // <- nenhum espaço antes ou depois do =
for(let i=0;i<n;i++) {result*=x;} // <- nenhum espaço
// o conteúdo de { ... } deveria estar numa nova linha
return result;
}

let x=prompt("x?",''), n=prompt("n?",'') // <-- technically possible,
// but better make it 2 lines, also there's no spaces and missing ;
if (n<0) // <- no spaces inside (n < 0), and should be extra line above it
{ // <- figure bracket on a separate line
// below - long lines can be split into multiple lines for improved readability
alert(`Power ${n} is not supported, please enter an integer number greater than zero`);
let x=prompt("x?",''), n=prompt("n?",'') // <-- tecnicamente possível,
// mas o melhor é torná-la em 2 linhas, também não existem espaços e falta o ;
if (n<0) // <- nenhum espaço dentro (n < 0), e deveria existir uma linha extra sobre a condição
{ // <- chaveta de abertura numa linha em separado
// abaixo - linhas longas podem ser repartidas por múltiplas linhas para melhorar a legíbilidade
alert(`A potência de ${n} não é suportada, por favor insira um número inteiro maior do que zero`);
}
else // <- could write it on a single line like "} else {"
else // <- poderia ser escrito numa única linha, como "} else {"
{
alert(pow(x,n)) // no spaces and missing ;
alert(pow(x,n)) // nenhum espaço e falta o ;
}
```

The fixed variant:
A variante modificada:

```js
function pow(x, n) {
Expand All @@ -40,8 +40,7 @@ let x = prompt("x?", "");
let n = prompt("n?", "");

if (n < 0) {
alert(`Power ${n} is not supported,
please enter an integer number greater than zero`);
alert(`A potência de ${n} não é suportada, por favor insira um número inteiro maior do que zero`);
} else {
alert( pow(x, n) );
}
Expand Down
8 changes: 4 additions & 4 deletions 1-js/03-code-quality/02-coding-style/1-style-errors/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 4

---

# Bad style
# Mau estilo

What's wrong with the code style below?
O que há de errado com o código abaixo?

```js no-beautify
function pow(x,n)
Expand All @@ -17,12 +17,12 @@ function pow(x,n)
let x=prompt("x?",''), n=prompt("n?",'')
if (n<=0)
{
alert(`Power ${n} is not supported, please enter an integer number greater than zero`);
alert(`A potência de ${n} não é suportada, por favor insira um número inteiro maior do que zero`);
}
else
{
alert(pow(x,n))
}
```

Fix it.
Modifique-o.
Loading