Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Array methods#155
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
Array methods#155
Changes fromall commits
Commits
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
14 changes: 7 additions & 7 deletions1-js/05-data-types/05-array-methods/1-camelcase/_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,10 +1,10 @@ | ||
functioncamelizace(řetězec) { | ||
returnřetězec | ||
.split('-') //rozdělí 'mé-dlouhé-slovo' na pole ['mé', 'dlouhé', 'slovo'] | ||
.map( | ||
//převede první písmena všech prvků pole kromě prvního na velká | ||
//převede ['mé', 'dlouhé', 'slovo']na ['mé', 'Dlouhé', 'Slovo'] | ||
(slovo, index) => index == 0 ?slovo :slovo[0].toUpperCase() +slovo.slice(1) | ||
) | ||
.join(''); //spojí ['mé', 'Dlouhé', 'Slovo']do 'méDlouhéSlovo' | ||
} |
18 changes: 9 additions & 9 deletions1-js/05-data-types/05-array-methods/1-camelcase/_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
describe("camelizace", function() { | ||
it("prázdný řádek nechá tak, jak je", function() { | ||
assert.equal(camelizace(""), ""); | ||
}); | ||
it("změní background-colorna backgroundColor", function() { | ||
assert.equal(camelizace("background-color"), "backgroundColor"); | ||
}); | ||
it("změní list-style-imagena listStyleImage", function() { | ||
assert.equal(camelizace("list-style-image"), "listStyleImage"); | ||
}); | ||
it("změní -webkit-transitionna WebkitTransition", function() { | ||
assert.equal(camelizace("-webkit-transition"), "WebkitTransition"); | ||
}); | ||
}); |
16 changes: 8 additions & 8 deletions1-js/05-data-types/05-array-methods/1-camelcase/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
14 changes: 7 additions & 7 deletions1-js/05-data-types/05-array-methods/10-average-age/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,14 +1,14 @@ | ||
```js run | ||
functionvraťPrůměrnýVěk(uživatelé) { | ||
returnuživatelé.reduce((předchozí, uživatel) =>předchozí +uživatel.věk, 0) /uživatelé.length; | ||
} | ||
letjan = {jméno: "Jan",věk: 25 }; | ||
letpetr = {jméno: "Petr",věk: 30 }; | ||
letmarie = {jméno: "Marie",věk: 29 }; | ||
letpole = [jan, petr, marie ]; | ||
alert(vraťPrůměrnýVěk(pole) ); // 28 | ||
``` | ||
18 changes: 9 additions & 9 deletions1-js/05-data-types/05-array-methods/10-average-age/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/05-array-methods/11-array-unique/_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,11 +1,11 @@ | ||
functionunikát(pole) { | ||
letvýsledek = []; | ||
for (letřetězec ofpole) { | ||
if (!výsledek.includes(řetězec)) { | ||
výsledek.push(řetězec); | ||
} | ||
} | ||
returnvýsledek; | ||
} |
18 changes: 9 additions & 9 deletions1-js/05-data-types/05-array-methods/11-array-unique/_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
describe("unikát", function() { | ||
it("odstraní neunikátní prvky", function() { | ||
letřetězce = ["Haré", "Kršna", "Haré", "Kršna", | ||
"Kršna", "Kršna", "Haré", "Haré", ":-O" | ||
]; | ||
assert.deepEqual(unikát(řetězce), ["Haré", "Kršna", ":-O"]); | ||
}); | ||
it("nemění zdrojové pole", function() { | ||
letřetězce = ["Kršna", "Kršna", "Haré", "Haré"]; | ||
unikát(řetězce); | ||
assert.deepEqual(řetězce, ["Kršna", "Kršna", "Haré", "Haré"]); | ||
}); | ||
}); |
40 changes: 20 additions & 20 deletions1-js/05-data-types/05-array-methods/11-array-unique/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,39 +1,39 @@ | ||
Projděme si prvky pole: | ||
-U každého prvku ověříme, zda výsledné pole již tento prvek obsahuje. | ||
-Pokud je tomu tak, budeme ho ignorovat, jinak ho přidáme do výsledku. | ||
```js run demo | ||
functionunikát(pole) { | ||
letvýsledek = []; | ||
for (letřetězec ofpole) { | ||
if (!výsledek.includes(řetězec)) { | ||
výsledek.push(řetězec); | ||
} | ||
} | ||
returnvýsledek; | ||
} | ||
letřetězce = ["Haré", "Kršna", "Haré", "Kršna", | ||
"Kršna", "Kršna", "Haré", "Haré", ":-O" | ||
]; | ||
alert(unikát(řetězce) ); //Haré, Kršna, :-O | ||
``` | ||
Tento kód funguje, ale má potenciální problém s výkonem. | ||
Metoda `výsledek.includes(řetězec)`vnitřně prochází pole `výsledek` a porovná každý jeho prvek s `řetězec`, aby našla shodu. | ||
Jestliže tedy v poli `výsledek` je `100` prvků a žádný se nerovná `řetězec`,pak projde celé pole `výsledek` a provede právě `100`porovnání. A je-li `výsledek` velký, např. `10000`,pak se vykoná`10000`porovnání. | ||
To samo o sobě není problém, jelikož JavaScriptové motory jsou velmi rychlé, takže projít pole`10000`prvků je otázkou mikrosekund. | ||
My však provádíme takový testpro každý prvek `pole` v cyklu `for`. | ||
Jestliže tedy `pole.length`je `10000`, budeme mít něco jako `10000*10000` = 100miliónů porovnání. To je hodně. | ||
Toto řešení je tedy dobré jen pro malá pole. | ||
Později v kapitole<info:map-set>uvidíme, jak je optimalizovat. |
18 changes: 9 additions & 9 deletions1-js/05-data-types/05-array-methods/11-array-unique/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
6 changes: 3 additions & 3 deletions1-js/05-data-types/05-array-methods/12-reduce-object/_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,6 +1,6 @@ | ||
functionseskupPodleId(pole) { | ||
returnpole.reduce((obj,hodnota) => { | ||
obj[hodnota.id] =hodnota; | ||
return obj; | ||
}, {}) | ||
} |
26 changes: 13 additions & 13 deletions1-js/05-data-types/05-array-methods/12-reduce-object/_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
describe("seskupPodleId", function() { | ||
it("vytvoří objekt seskupený podle id", function() { | ||
letuživatelé = [ | ||
{id: 'jan',jméno: "Jan Novák",věk: 20}, | ||
{id: 'anna',jméno: "Anna Nováková",věk: 24}, | ||
{id: 'petr',jméno: "Petr Petřík",věk: 31}, | ||
]; | ||
assert.deepEqual(seskupPodleId(uživatelé), { | ||
jan: {id: 'jan',jméno: "Jan Novák",věk: 20}, | ||
anna: {id: 'anna',jméno: "Anna Nováková",věk: 24}, | ||
petr: {id: 'petr',jméno: "Petr Petřík",věk: 31}, | ||
}); | ||
}); | ||
it("funguje s prázdným polem", function() { | ||
uživatelé = []; | ||
assert.deepEqual(seskupPodleId(uživatelé), {}); | ||
}); | ||
}); |
34 changes: 17 additions & 17 deletions1-js/05-data-types/05-array-methods/12-reduce-object/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
6 changes: 3 additions & 3 deletions1-js/05-data-types/05-array-methods/2-filter-range/_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,5 +1,5 @@ | ||
functionfiltrujPodleRozsahu(pole, a, b) { | ||
//závorky kolem výrazu jsou přidány pro lepší čitelnost | ||
returnpole.filter(prvek => (a <=prvek &&prvek <= b)); | ||
} |
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.