Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Operators #147

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

Open
Gabitzzz wants to merge 13 commits into javascript-tutorial:master
base: master
Choose a base branch
Loading
from Gabitzzz:master
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
2759c78
Update solution.md
Gabitzzz Nov 27, 2022
ed8131a
Update task.md
Gabitzzz Nov 27, 2022
9d89f2e
Update article.md
Gabitzzz Nov 27, 2022
8cf43af
Update article.md
Gabitzzz Nov 28, 2022
bd196df
Update article.md
Gabitzzz Nov 28, 2022
7fcf68d
Update 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
Gabitzzz Nov 28, 2022
3187282
Update 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
Gabitzzz Nov 28, 2022
ded9060
Update 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
Gabitzzz Nov 28, 2022
2720578
Update 1-js/02-first-steps/08-operators/article.md
Gabitzzz Nov 28, 2022
4ff45cc
Update 1-js/02-first-steps/08-operators/article.md
Gabitzzz Nov 28, 2022
41866b9
Update 1-js/02-first-steps/08-operators/article.md
Gabitzzz Nov 28, 2022
bc536b9
Update 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
Gabitzzz Nov 28, 2022
2d28aa6
Update 1-js/02-first-steps/08-operators/article.md
Gabitzzz Nov 28, 2022
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
25 changes: 12 additions & 13 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
The reason is that prompt returns user input as a string.

Copy link
Contributor

@bogdanbacosca bogdanbacosca Nov 28, 2022

Choose a reason for hiding this comment

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

După instrucțiuni, trebuie să păstrăm liniile "așa cum sunt". Acest lucru reduce conflictele de merge.
Sugestie: adaugă o linie goală între linia 1 și 2.

So variables have values `"1"` and `"2"` respectively.
Motivul este că prompt returnează inputul utilizatorului ca și un șir.
Astfel variable au valorile `"1"` și `"2"` respectiv.

```js run
let a = "1"; // prompt("First number?", 1);
let b = "2"; // prompt("Second number?", 2);
let a = "1"; // prompt("Primul număr?", 1);
let b = "2"; // prompt("Al doilea număr?", 2);

alert(a + b); // 12
```

What we should do is to convert strings to numbers before `+`. For example, using `Number()` or prepending them with `+`.
Ce ar trebui noi să facem este să convertim șirurile în numere înainte de `+`. De exemplu, folosind `Number()` sau să le adăugam `+` în față.

For example, right before `prompt`:
De exemplu, chiar înainte de `prompt`:

```js run
let a = +prompt("First number?", 1);
let b = +prompt("Second number?", 2);
let a = +prompt("Primul număr?", 1);
let b = +prompt("Al doilea număr?", 2);

alert(a + b); // 3
```

Or in the `alert`:
Sau în `alert`:

```js run
let a = prompt("First number?", 1);
let b = prompt("Second number?", 2);
let a = prompt("Primul număr?", 1);
let b = prompt("Al doilea număr?", 2);

alert(+a + +b); // 3
```

Using both unary and binary `+` in the latest code. Looks funny, doesn't it?
Folosind atât `+` unar cât și binar în cel mai recent cod. Arată amuzant, nu-i așa?
8 changes: 4 additions & 4 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/task.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ importance: 5

---

# Fix the addition
# Rezolvă adunarea

Here's a code that asks the user for two numbers and shows their sum.
Iată un cod care îi cere utilizatorului două numere și afișează suma lor.

It works incorrectly. The output in the example below is `12` (for default prompt values).
Funcționează incorect. Rezultatul în răspunsul de mai jos este `12` (pentru valorile implicite din prompt).

Why? Fix it. The result should be `3`.
De ce? Rezolvă. Rezultatul ar trebui să fie `3`.

```js run
let a = prompt("First number?", 1);
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /