FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Operators by Gabitzzz · Pull Request #147 · javascript-tutorial/ro.javascript.info · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .md  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • solution.md
      • task.md
    • article.md
25 changes: 12 additions & 13 deletions 1-js/02-first-steps/08-operators/4-fix-prompt/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL