Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Numbers#152
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Numbers#152
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
8 changes: 4 additions & 4 deletions1-js/05-data-types/02-number/1-sum-interface/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,12 +1,12 @@ | ||
```js run demo | ||
let a = +prompt("První číslo?", ""); | ||
let b = +prompt("Druhé číslo?", ""); | ||
alert( a + b ); | ||
``` | ||
Všimněte si unárního plus `+`před `prompt`, které okamžitě konvertuje hodnotu na číslo. | ||
Jinak by`a`a `b`byly řetězce a součtem by bylo jejich zřetězení, tedy: `"1" + "2" = "12"`. |
6 changes: 3 additions & 3 deletions1-js/05-data-types/02-number/1-sum-interface/task.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
18 changes: 9 additions & 9 deletions1-js/05-data-types/02-number/2-why-rounded-down/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,33 +1,33 @@ | ||
Interně je desetinné číslo `6.35`nekonečné binární číslo. Jako vždy v takových případech je uloženo se ztrátou přesnosti. | ||
Podívejme se: | ||
```js run | ||
alert( 6.35.toFixed(20) ); // 6.34999999999999964473 | ||
``` | ||
Ztrátou přesnosti se číslo může zvýšit i snížit. V tomto konkrétním případě se číslo o něco málo sníží, proto bude zaokrouhleno dolů. | ||
A co `1.35`? | ||
```js run | ||
alert( 1.35.toFixed(20) ); // 1.35000000000000008882 | ||
``` | ||
Zde ztráta přesnosti číslo trošičku zvýšila, takže se zaokrouhlilo nahoru. | ||
**Jak můžeme problém s`6.35`vyřešit, chceme-li, aby se zaokrouhlilo správně?** | ||
Měli bychom je před zaokrouhlením přiblížit k celému číslu: | ||
```js run | ||
alert( (6.35 * 10).toFixed(20) ); // 63.50000000000000000000 | ||
``` | ||
Všimněte si, že`63.5`nemá vůbec žádnou ztrátu přesnosti. Je to proto, že desetinná část`0.5`je ve skutečnosti`1/2`.Zlomky s mocninou`2`ve jmenovateli jsou v binární soustavě reprezentovány přesně, takže je nyní můžeme zaokrouhlit: | ||
```js run | ||
alert( Math.round(6.35 * 10) / 10 ); // 6.35 -> 63.5 -> 64(zaokrouhleno) -> 6.4 | ||
``` | ||
10 changes: 5 additions & 5 deletions1-js/05-data-types/02-number/2-why-rounded-down/task.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
12 changes: 6 additions & 6 deletions1-js/05-data-types/02-number/3-repeat-until-number/_js.view/solution.js
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,12 +1,12 @@ | ||
functionnačtiČíslo() { | ||
letčíslo; | ||
do { | ||
číslo = prompt("Zadejte číslo, prosím:", 0); | ||
} while ( !isFinite(číslo) ); | ||
if (číslo === null ||číslo === '') return null; | ||
return +číslo; | ||
} |
26 changes: 13 additions & 13 deletions1-js/05-data-types/02-number/3-repeat-until-number/_js.view/test.js
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
20 changes: 10 additions & 10 deletions1-js/05-data-types/02-number/3-repeat-until-number/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,23 +1,23 @@ | ||
```js run demo | ||
functionnačtiČíslo() { | ||
letčíslo; | ||
do { | ||
číslo = prompt("Zadejte číslo, prosím:", 0); | ||
} while ( !isFinite(číslo) ); | ||
if (číslo === null ||číslo === '') return null; | ||
return +číslo; | ||
} | ||
alert(`Načteno: ${načtiČíslo()}`); | ||
``` | ||
Řešení je trochu složitější, než by mohlo být, protože si musíme poradit s`null`/prázdnými řádky. | ||
Ve skutečnosti tedy přijímáme vstup tak dlouho, dokud to není „skutečné číslo“. Tuto podmínku splňují i`null` (storno) a prázdný řádek, protože v číselné podobě jsou obě `0`. | ||
Po skončení musíme zacházet s`null`a s prázdným řádkem speciálně (vrátit `null`),jelikož jejich konverze na číslo by vrátila `0`. | ||
8 changes: 4 additions & 4 deletions1-js/05-data-types/02-number/3-repeat-until-number/task.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
10 changes: 5 additions & 5 deletions1-js/05-data-types/02-number/4-endless-loop-error/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
4 changes: 2 additions & 2 deletions1-js/05-data-types/02-number/4-endless-loop-error/task.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 |
---|---|---|
@@ -2,9 +2,9 @@ importance: 4 | ||
--- | ||
#Občasná nekonečná smyčka | ||
Tento cyklus je nekonečný. Nikdy neskončí. Proč? | ||
```js | ||
let i = 0; | ||
10 changes: 5 additions & 5 deletions1-js/05-data-types/02-number/8-random-min-max/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
8 changes: 4 additions & 4 deletions1-js/05-data-types/02-number/8-random-min-max/task.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
50 changes: 25 additions & 25 deletions1-js/05-data-types/02-number/9-random-int-min-max/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,66 +1,66 @@ | ||
#Jednoduché, ale nesprávné řešení | ||
Jednoduchým, ale nesprávným řešením by bylo generovat hodnotu od`min`do `max`a zaokrouhlit ji: | ||
```js run | ||
function randomInteger(min, max) { | ||
letnáhodnéČíslo = min + Math.random() * (max - min); | ||
return Math.round(náhodnéČíslo); | ||
} | ||
alert( randomInteger(1, 3) ); | ||
``` | ||
Tato funkce funguje, ale nekorektně. Pravděpodobnost, že získáme krajní hodnoty`min`a `max`, je dvakrát nižší, než u ostatních hodnot. | ||
Jestliže si spustíte výše uvedený příklad mnohokrát po sobě, brzy uvidíte, že nejčastěji se objevuje`2`. | ||
Děje se to proto, že`Math.round()`získává náhodná čísla z intervalu`1..3`a zaokrouhluje je následovně: | ||
```js no-beautify | ||
hodnoty od 1 ...do 1.4999999999se zaokrouhlí na 1 | ||
hodnoty od 1.5 ...do 2.4999999999se zaokrouhlí na 2 | ||
hodnoty od 2.5 ...do 2.9999999999se zaokrouhlí na 3 | ||
``` | ||
Nyní jasně vidíme, že`1`má dvakrát méně hodnot než `2`.Totéž platí pro `3`. | ||
#Správné řešení | ||
Tato úloha má mnoho správných řešení. Jedno z nich je přizpůsobit hranice intervalu. Abychom zajistili stejné intervaly, můžeme generovat hodnoty od`0.5` do `3.5` a tím zvýšit požadované pravděpodobnosti krajních hodnot: | ||
```js run | ||
*!* | ||
function randomInteger(min, max) { | ||
//nyní náhodnéČíslo je od(min-0.5)do (max+0.5) | ||
letnáhodnéČíslo = min - 0.5 + Math.random() * (max - min + 1); | ||
return Math.round(náhodnéČíslo); | ||
} | ||
*/!* | ||
alert( randomInteger(1, 3) ); | ||
``` | ||
Alternativním způsobem by bylo použít`Math.floor`pro náhodné číslo od`min`do `max+1`: | ||
```js run | ||
*!* | ||
function randomInteger(min, max) { | ||
//zde náhodnéČíslo je od mindo (max+1) | ||
letnáhodnéČíslo = min + Math.random() * (max + 1 - min); | ||
return Math.floor(náhodnéČíslo); | ||
} | ||
*/!* | ||
alert( randomInteger(1, 3) ); | ||
``` | ||
Nyní jsou všechny intervaly mapovány tímto způsobem: | ||
```js no-beautify | ||
hodnoty od 1 ...do 1.9999999999se zaokrouhlí na 1 | ||
hodnoty od 2 ...do 2.9999999999se zaokrouhlí na 2 | ||
hodnoty od 3 ...do 3.9999999999se zaokrouhlí na 3 | ||
``` | ||
Všechny intervaly mají stejnou délku, takže konečné rozložení je rovnoměrné. |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.