Movatterモバイル変換


[0]ホーム

URL:


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

Arrays#154

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
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.5.4
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
12 changes: 6 additions & 6 deletions1-js/05-data-types/04-array/1-item-value/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
The result is `4`:
Výsledek je `4`:


```js run
letfruits = ["Apples", "Pear", "Orange"];
letovoce = ["Jablko", "Hruška", "Pomeranč"];

letshoppingCart =fruits;
letnákupníKošík =ovoce;

shoppingCart.push("Banana");
nákupníKošík.push("Banán");

*!*
alert(fruits.length ); // 4
alert(ovoce.length ); // 4
*/!*
```

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.
Je to tím, že pole jsou objekty. Proto jsou `nákupníKošík` i `ovoce` odkazy na totéž pole.

16 changes: 8 additions & 8 deletions1-js/05-data-types/04-array/1-item-value/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,18 +2,18 @@ importance: 3

---

#Is array copied?
#Zkopíruje se pole?

What is this code going to show?
Co zobrazí tento kód?

```js
letfruits = ["Apples", "Pear", "Orange"];
letovoce = ["Jablko", "Hruška", "Pomeranč"];

//push a new value into the "copy"
letshoppingCart =fruits;
shoppingCart.push("Banana");
//přidáme do „kopie“ novou hodnotu
letnákupníKošík =ovoce;
nákupníKošík.push("Banán");

//what's in fruits?
alert(fruits.length ); // ?
//co bude v poli ovoce?
alert(ovoce.length ); // ?
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
functiongetMaxSubSum(arr) {
letmaxSum = 0;
letpartialSum = 0;
functionvraťMaxSoučetPodpole(arr) {
letmaxSoučet = 0;
letčástečnýSoučet = 0;

for (letitem ofarr) {
partialSum +=item;
maxSum = Math.max(maxSum, partialSum);
if (partialSum < 0)partialSum = 0;
for (letprvek ofpole) {
částečnýSoučet +=prvek;
maxSoučet = Math.max(maxSoučet, částečnýSoučet);
if (částečnýSoučet < 0)částečnýSoučet = 0;
}
returnmaxSum;
returnmaxSoučet;
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
describe("getMaxSubSum", function() {
it("maximal subsum of[1, 2, 3]equals 6", function() {
assert.equal(getMaxSubSum([1, 2, 3]), 6);
describe("vraťMaxSoučetPodpole", function() {
it("maximální podsoučet[1, 2, 3]se rovná 6", function() {
assert.equal(vraťMaxSoučetPodpole([1, 2, 3]), 6);
});

it("maximal subsum of[-1, 2, 3, -9]equals 5", function() {
assert.equal(getMaxSubSum([-1, 2, 3, -9]), 5);
it("maximální podsoučet[-1, 2, 3, -9]se rovná 5", function() {
assert.equal(vraťMaxSoučetPodpole([-1, 2, 3, -9]), 5);
});

it("maximal subsum of[-1, 2, 3, -9, 11]equals 11", function() {
assert.equal(getMaxSubSum([-1, 2, 3, -9, 11]), 11);
it("maximální podsoučet[-1, 2, 3, -9, 11]se rovná 11", function() {
assert.equal(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]), 11);
});

it("maximal subsum of[-2, -1, 1, 2]equals 3", function() {
assert.equal(getMaxSubSum([-2, -1, 1, 2]), 3);
it("maximální podsoučet[-2, -1, 1, 2]se rovná 3", function() {
assert.equal(vraťMaxSoučetPodpole([-2, -1, 1, 2]), 3);
});

it("maximal subsum of[100, -9, 2, -3, 5]equals 100", function() {
assert.equal(getMaxSubSum([100, -9, 2, -3, 5]), 100);
it("maximální podsoučet[100, -9, 2, -3, 5]se rovná 100", function() {
assert.equal(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]), 100);
});

it("maximal subsum of[]equals 0", function() {
assert.equal(getMaxSubSum([]), 0);
it("maximální podsoučet[]se rovná 0", function() {
assert.equal(vraťMaxSoučetPodpole([]), 0);
});

it("maximal subsum of[-1]equals 0", function() {
assert.equal(getMaxSubSum([-1]), 0);
it("maximální podsoučet[-1]se rovná 0", function() {
assert.equal(vraťMaxSoučetPodpole([-1]), 0);
});

it("maximal subsum of[-1, -2]equals 0", function() {
assert.equal(getMaxSubSum([-1, -2]), 0);
it("maximální podsoučet[-1, -2]se rovná 0", function() {
assert.equal(vraťMaxSoučetPodpole([-1, -2]), 0);
});

it("maximal subsum of[2, -8, 5, -1, 2, -3, 2]equals 6", function() {
assert.equal(getMaxSubSum([2, -8, 5, -1, 2, -3, 2]), 6);
it("maximální podsoučet[2, -8, 5, -1, 2, -3, 2]se rovná 6", function() {
assert.equal(vraťMaxSoučetPodpole([2, -8, 5, -1, 2, -3, 2]), 6);
});
});
92 changes: 46 additions & 46 deletions1-js/05-data-types/04-array/10-maximal-subarray/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
#Slow solution
#Pomalé řešení

We can calculate all possible subsums.
Můžeme spočítat všechny možné podsoučty.

The simplest way is to take every element and calculate sums of all subarrays starting from it.
Nejjednodušším způsobem je vzít každý prvek a počítat součty všech podpolí, která jím začínají.

For instance, for `[-1, 2, 3, -9, 11]`:
Například pro `[-1, 2, 3, -9, 11]`:

```js no-beautify
//Starting from -1:
//Začínající -1:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

//Starting from 2:
//Začínající 2:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

//Starting from 3:
//Začínající 3:
3
3 + (-9)
3 + (-9) + 11

//Starting from -9
//Začínající -9:
-9
-9 + 11

//Starting from 11
//Začínající 11:
11
```

The code is actually a nested loop: the external loop over array elements, and the internal counts subsums starting with the current element.
Kód je ve skutečnosti vnořený cyklus: vnější cyklus prochází prvky pole, vnitřní počítá podsoučty počínaje aktuálním prvkem.

```js run
functiongetMaxSubSum(arr) {
letmaxSum = 0; //if we take no elements, zero will be returned

for (let i = 0; i <arr.length; i++) {
letsumFixedStart = 0;
for (let j = i; j <arr.length; j++) {
sumFixedStart +=arr[j];
maxSum = Math.max(maxSum, sumFixedStart);
functionvraťMaxSoučetPodpole(pole) {
letmaxSoučet = 0; //nevezmeme-li žádné prvky, vrátí se nula

for (let i = 0; i <pole.length; i++) {
letsoučetPevnýZačátek = 0;
for (let j = i; j <pole.length; j++) {
součetPevnýZačátek +=pole[j];
maxSoučet = Math.max(maxSoučet, součetPevnýZačátek);
}
}

returnmaxSum;
returnmaxSoučet;
}

alert(getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert(getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert(getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert(getMaxSubSum([1, 2, 3]) ); // 6
alert(getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9]) ); // 5
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]) ); // 11
alert(vraťMaxSoučetPodpole([-2, -1, 1, 2]) ); // 3
alert(vraťMaxSoučetPodpole([1, 2, 3]) ); // 6
alert(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]) ); // 100
```

The solution has a time complexity of[O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation).In other words, if we increase the array size 2 times, the algorithm will work 4 times longer.
Toto řešení má časovou složitost[O(n<sup>2</sup>)](https://cs.wikipedia.org/wiki/Landauova_notace).Jinými slovy, když zvětšíme pole dvojnásobně, algoritmus bude pracovat čtyřikrát déle.

For big arrays (1000, 10000or more items) such algorithms can lead to serious sluggishness.
Pro velká pole (1000, 10000nebo více prvků) mohou takové algoritmy vést k vážnému zpomalení.

#Fast solution
#Rychlé řešení

Let's walk the array and keep the current partial sum of elements in the variable`s`.If `s`becomes negative at some point, then assign`s=0`.Themaximumof all such`s` will be the answer.
Budeme procházet prvky pole a pamatovat si aktuální částečný součet prvků v proměnné`s`.Bude-li `s`v některém bodě záporné, přiřadíme`s=0`.Odpovědí budemaximumvšech takových`s`.

If the description is too vague, please see the code, it's short enough:
Pokud je popis příliš vágní, prosíme nahlédněte do kódu, je dosti krátký:

```js run demo
functiongetMaxSubSum(arr) {
letmaxSum = 0;
letpartialSum = 0;

for (letitem ofarr) { //for each item of arr
partialSum +=item; //add it to partialSum
maxSum = Math.max(maxSum, partialSum); //remember the maximum
if (partialSum < 0)partialSum = 0; //zero if negative
functionvraťMaxSoučetPodpole(pole) {
letmaxSoučet = 0;
letčástečnýSoučet = 0;

for (letprvek ofpole) { //pro každý prvek pole
částečnýSoučet +=prvek; //přičteme jej do částečnýSoučet
maxSoučet = Math.max(maxSoučet, částečnýSoučet); //zapamatujeme si maximum
if (částečnýSoučet < 0)částečnýSoučet = 0; //je-li součet záporný, vynulujeme ho
}

returnmaxSum;
returnmaxSoučet;
}

alert(getMaxSubSum([-1, 2, 3, -9]) ); // 5
alert(getMaxSubSum([-1, 2, 3, -9, 11]) ); // 11
alert(getMaxSubSum([-2, -1, 1, 2]) ); // 3
alert(getMaxSubSum([100, -9, 2, -3, 5]) ); // 100
alert(getMaxSubSum([1, 2, 3]) ); // 6
alert(getMaxSubSum([-1, -2, -3]) ); // 0
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9]) ); // 5
alert(vraťMaxSoučetPodpole([-1, 2, 3, -9, 11]) ); // 11
alert(vraťMaxSoučetPodpole([-2, -1, 1, 2]) ); // 3
alert(vraťMaxSoučetPodpole([100, -9, 2, -3, 5]) ); // 100
alert(vraťMaxSoučetPodpole([1, 2, 3]) ); // 6
alert(vraťMaxSoučetPodpole([-1, -2, -3]) ); // 0
```

The algorithm requires exactly 1array pass, so the time complexity is O(n).
Tento algoritmus vyžaduje přesně 1průchod polem, takže jeho časová složitost je O(n).

You can find more detailed information about the algorithm here: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem).If it's still not obvious why that works, then please trace the algorithm on the examples above, see how it works, that's better than any words.
Podrobnější informace o algoritmu můžete najít zde: [Maximum subarray problem (Problém maximálního podpole)](http://en.wikipedia.org/wiki/Maximum_subarray_problem).Není-li vám stále jasné, proč to funguje, potom si prosíme projděte algoritmus na výše uvedených příkladech a podívejte se, jak funguje. Je to lepší než jakákoli slova.
28 changes: 14 additions & 14 deletions1-js/05-data-types/04-array/10-maximal-subarray/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,29 +2,29 @@ importance: 2

---

#A maximal subarray
#Maximální podpole

The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
Na vstupu je pole čísel, např. `pole = [1, -2, 3, 4, -9, 6]`.

The task is: find the contiguous subarray of `arr` with the maximal sum of items.
Úkol zní: najděte souvislé podpole tohoto pole s maximálním součtem prvků.

Write the function `getMaxSubSum(arr)` that will return that sum.
Napište funkci `vraťMaxSoučetPodpole(pole)`, která tento součet vrátí.

For instance:
Příklad:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (the sum of highlighted items)
getMaxSubSum([*!*2, -1, 2, 3*/!*, -9]) == 6
getMaxSubSum([-1, 2, 3, -9, *!*11*/!*]) == 11
getMaxSubSum([-2, -1, *!*1, 2*/!*]) == 3
getMaxSubSum([*!*100*/!*, -9, 2, -3, 5]) == 100
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (take all)
vraťMaxSoučetPodpole([-1, *!*2, 3*/!*, -9]) == 5 (součet zvýrazněných prvků)
vraťMaxSoučetPodpole([*!*2, -1, 2, 3*/!*, -9]) == 6
vraťMaxSoučetPodpole([-1, 2, 3, -9, *!*11*/!*]) == 11
vraťMaxSoučetPodpole([-2, -1, *!*1, 2*/!*]) == 3
vraťMaxSoučetPodpole([*!*100*/!*, -9, 2, -3, 5]) == 100
vraťMaxSoučetPodpole([*!*1, 2, 3*/!*]) == 6 (vezmi vše)
```

If all items are negative, it means that we take none (the subarray is empty),so the sum is zero:
Jsou-li všechny prvky záporné, znamená to, že nevezmeme žádný (podpole je prázdné),takže součet je nulový:

```js
getMaxSubSum([-1, -2, -3]) = 0
vraťMaxSoučetPodpole([-1, -2, -3]) = 0
```

Please try to think of a fast solution: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) or even O(n) if you can.
Snažte se prosíme vymyslet rychlé řešení: [O(n<sup>2</sup>)](https://cs.wikipedia.org/wiki/Landauova_notace) nebo dokonce O(n), jestliže to dokážete.
10 changes: 5 additions & 5 deletions1-js/05-data-types/04-array/2-create-array/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@


```js run
letstyles = ["Jazz", "Blues"];
styles.push("Rock-n-Roll");
styles[Math.floor((styles.length - 1) / 2)] = "Classics";
alert(styles.shift() );
styles.unshift("Rap", "Reggae");
letstyly = ["Jazz", "Blues"];
styly.push("Rock-n-Roll");
styly[Math.floor((styly.length - 1) / 2)] = "Klasika";
alert(styly.shift() );
styly.unshift("Rap", "Reggae");
```

22 changes: 11 additions & 11 deletions1-js/05-data-types/04-array/2-create-array/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,23 +2,23 @@ importance: 5

---

#Array operations.
#Operace s poli

Let's try 5array operations.
Zkusme provést s polem 5operací.

1.Create an array `styles` with items "Jazz" and "Blues".
2.Append "Rock-n-Roll" to the end.
3.Replace the value in the middle with "Classics". Your code for finding the middle value should work for any arrays with odd length.
4.Strip off the first value of the array and show it.
5.Prepend `Rap` and `Reggae` to the array.
1.Vytvořte pole `styly` s prvky „Jazz“ a „Blues.
2.Připojte na konec „Rock-n-Roll.
3.Nahraďte prostřední hodnotu prvkem „Klasika“. Váš kód pro nalezení prostřední hodnoty by měl fungovat pro všechna pole liché délky.
4.Vyjměte z pole první hodnotu a zobrazte ji.
5.Připojte na začátek pole „Rap“ a „Reggae“.

The array in the process:
Pole během tohoto procesu:

```js no-beautify
Jazz, Blues
Jazz, Blues, Rock-n-Roll
Jazz,Classics, Rock-n-Roll
Classics, Rock-n-Roll
Rap, Reggae,Classics, Rock-n-Roll
Jazz,Klasika, Rock-n-Roll
Klasika, Rock-n-Roll
Rap, Reggae,Klasika, Rock-n-Roll
```

12 changes: 6 additions & 6 deletions1-js/05-data-types/04-array/3-call-array-this/solution.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
The call `arr[2]()`is syntactically the good old`obj[method]()`,in the role of`obj`we have `arr`, and in the role of `method` we have `2`.
Volání `pole[2]()`je syntakticky stará dobrá`obj[metoda]()`,v roli`obj`máme `pole` a v roli `metoda` máme `2`.

So we have a call of the function `arr[2]`as an object method. Naturally, it receives `this`referencing the object `arr` and outputs the array:
Zavolali jsme tedy funkci `pole[2]`jako objektovou metodu. Ta přirozeně obdrží `this`odkazující se na objekt `pole` a vypíše toto pole:

```js run
letarr = ["a", "b"];
letpole = ["a", "b"];

arr.push(function() {
pole.push(function() {
alert( this );
})

arr[2](); // a,b,function(){...}
pole[2](); // a,b,function(){...}
```

The array has 3values: initially it had two, plusthe function.
Toto pole má 3hodnoty: na začátku mělo dvě, plusfunkce.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp