Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 4 additions & 7 deletions 1-js/05-data-types/04-array/1-item-value/solution.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
The result is `4`:
Resultatet er `4`:


```js run
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["Æble", "Pære", "Appelsin"];

let shoppingCart = fruits;

shoppingCart.push("Banana");
shoppingCart.push("Banan");

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

That's because arrays are objects. So both `shoppingCart` and `fruits` are the references to the same array.

``` Det er fordi arrays er objekter. Så både `shoppingCart` og `fruits` er referencer til det samme array.
12 changes: 6 additions & 6 deletions 1-js/05-data-types/04-array/1-item-value/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ importance: 3

---

# Is array copied?
# Bliver array kopieret?

What is this code going to show?
Hvad vil denne kode vise?

```js
let fruits = ["Apples", "Pear", "Orange"];
let fruits = ["Æble", "Pære", "Appelsin"];

// push a new value into the "copy"
// skub push en ny værdi ind i "kopien"
let shoppingCart = fruits;
shoppingCart.push("Banana");
shoppingCart.push("Banan");

// what's in fruits?
// hvad er der i fruits?
alert( fruits.length ); // ?
```

20 changes: 10 additions & 10 deletions 1-js/05-data-types/04-array/10-maximal-subarray/_js.view/test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
describe("getMaxSubSum", function() {
it("maximal subsum of [1, 2, 3] equals 6", function() {
describe("getMaxSubSum", function () {
it("maksimal sum af [1, 2, 3] er 6", function () {
assert.equal(getMaxSubSum([1, 2, 3]), 6);
});

it("maximal subsum of [-1, 2, 3, -9] equals 5", function() {
it("maksimal sum af [-1, 2, 3, -9] er 5", function () {
assert.equal(getMaxSubSum([-1, 2, 3, -9]), 5);
});

it("maximal subsum of [-1, 2, 3, -9, 11] equals 11", function() {
it("maksimal sum af [-1, 2, 3, -9, 11] equals 11", function () {
assert.equal(getMaxSubSum([-1, 2, 3, -9, 11]), 11);
});

it("maximal subsum of [-2, -1, 1, 2] equals 3", function() {
it("maksimal sum af [-2, -1, 1, 2] equals 3", function () {
assert.equal(getMaxSubSum([-2, -1, 1, 2]), 3);
});

it("maximal subsum of [100, -9, 2, -3, 5] equals 100", function() {
it("maksimal sum af [100, -9, 2, -3, 5] er 100", function () {
assert.equal(getMaxSubSum([100, -9, 2, -3, 5]), 100);
});

it("maximal subsum of [] equals 0", function() {
it("maksimal sum af [] er 0", function () {
assert.equal(getMaxSubSum([]), 0);
});

it("maximal subsum of [-1] equals 0", function() {
it("maksimal sum af [-1] er 0", function () {
assert.equal(getMaxSubSum([-1]), 0);
});

it("maximal subsum of [-1, -2] equals 0", function() {
it("maksimal sum af [-1, -2] er 0", function () {
assert.equal(getMaxSubSum([-1, -2]), 0);
});

it("maximal subsum of [2, -8, 5, -1, 2, -3, 2] equals 6", function() {
it("maksimal sum af [2, -8, 5, -1, 2, -3, 2] er 6", function () {
assert.equal(getMaxSubSum([2, -8, 5, -1, 2, -3, 2]), 6);
});
});
45 changes: 22 additions & 23 deletions 1-js/05-data-types/04-array/10-maximal-subarray/solution.md
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
# Slow solution
# Langsom løsning

We can calculate all possible subsums.
Vi kan beregne alle mulige delsummer.

The simplest way is to take every element and calculate sums of all subarrays starting from it.

For instance, for `[-1, 2, 3, -9, 11]`:
Den simpleste måde er at tage hvert element og beregne summen af alle delarrays, der starter fra det.
For eksempel, for `[-1, 2, 3, -9, 11]`:

```js no-beautify
// Starting from -1:
// Startende fra -1:
-1
-1 + 2
-1 + 2 + 3
-1 + 2 + 3 + (-9)
-1 + 2 + 3 + (-9) + 11

// Starting from 2:
// Startende fra 2:
2
2 + 3
2 + 3 + (-9)
2 + 3 + (-9) + 11

// Starting from 3:
// Startende fra 3:
3
3 + (-9)
3 + (-9) + 11

// Starting from -9
// Startende fra -9
-9
-9 + 11

// Starting from 11
// Startende fra 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.
Koden er faktisk en indlejret løkke: den ydre løkke går over array-elementerne, og den indre tæller delsummer, der starter med det aktuelle element.

```js run
function getMaxSubSum(arr) {
let maxSum = 0; // if we take no elements, zero will be returned
let maxSum = 0; // hvis vi ikke tager nogle elementer, vil nul blive returneret

for (let i = 0; i < arr.length; i++) {
let sumFixedStart = 0;
Expand All @@ -57,25 +56,25 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([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.
Løsningen har en tidskompleksitet på [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation). Med andre ord, hvis vi fordobler størrelsen på arrayet, vil algoritmen tage fire gange så lang tid.

For big arrays (1000, 10000 or more items) such algorithms can lead to serious sluggishness.
For store arrays (1000, 10000 eller flere elementer) kan sådanne algoritmer føre til alvorlig langsommelighed.

# Fast solution
# Hurtig løsning

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`. The maximum of all such `s` will be the answer.
Lad os gå igennem arrayet og holde den nuværende delsum af elementer i variablen `s`. Hvis `s` bliver negativ på et tidspunkt, så sæt `s=0`. Maksimum af alle sådanne `s` vil være svaret.

If the description is too vague, please see the code, it's short enough:
Hvis beskrivelsen er for vag, så se venligst koden, den er kort nok:

```js run demo
function getMaxSubSum(arr) {
let maxSum = 0;
let partialSum = 0;

for (let item of arr) { // 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
for (let item of arr) { // for hvert item af arr
partialSum += item; // læg item til partialSum
maxSum = Math.max(maxSum, partialSum); // husk maksimum
if (partialSum < 0) partialSum = 0; // nul hvis negativ
}

return maxSum;
Expand All @@ -89,6 +88,6 @@ alert( getMaxSubSum([1, 2, 3]) ); // 6
alert( getMaxSubSum([-1, -2, -3]) ); // 0
```

The algorithm requires exactly 1 array pass, so the time complexity is O(n).
Algoritmen kræver præcis 1 gennemgang af arrayet, så tidskompleksiteten er 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.
Du kan finde mere detaljeret information om algoritmen her: [Maximum subarray problem](http://en.wikipedia.org/wiki/Maximum_subarray_problem). Hvis det stadig ikke er indlysende, hvorfor det virker, så prøv at følge algoritmen på eksemplerne ovenfor, se hvordan den fungerer, det er ofte bedre end ord.
16 changes: 8 additions & 8 deletions 1-js/05-data-types/04-array/10-maximal-subarray/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ importance: 2

# A maximal subarray

The input is an array of numbers, e.g. `arr = [1, -2, 3, 4, -9, 6]`.
Input er et array af tal, f.eks. `arr = [1, -2, 3, 4, -9, 6]`.

The task is: find the contiguous subarray of `arr` with the maximal sum of items.
Opgaven er: find det sammenhængende delarray af `arr` med den maksimale sum af elementer.

Write the function `getMaxSubSum(arr)` that will return that sum.
Skriv funktionen `getMaxSubSum(arr)`, der returnerer den sum.

For instance:
For eksempel:

```js
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (the sum of highlighted items)
getMaxSubSum([-1, *!*2, 3*/!*, -9]) == 5 (summen af de markerede elementer)
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)
getMaxSubSum([*!*1, 2, 3*/!*]) == 6 (tag det hele)
```

If all items are negative, it means that we take none (the subarray is empty), so the sum is zero:
Hvis alle elementer er negative, betyder det, at vi ikke tager nogen (delarrayet er tomt), så summen er nul:

```js
getMaxSubSum([-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.
Prøv at tænke på en hurtig løsning: [O(n<sup>2</sup>)](https://en.wikipedia.org/wiki/Big_O_notation) eller endda O(n), hvis du kan.
16 changes: 8 additions & 8 deletions 1-js/05-data-types/04-array/2-create-array/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ importance: 5

---

# Array operations.
# Array operationer.

Let's try 5 array operations.
Lad os prøve 5 array operationer.

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. Opret et array `styles` med elementerne "Jazz" og "Blues".
2. Tilføj "Rock-n-Roll" til slutningen.
3. Erstat værdien i midten med "Classics". Din kode til at finde midterværdien skal fungere for alle arrays med ulige længde.
4. Fjern den første værdi i arrayet og vis den.
5. Tilføj `Rap` og `Reggae` til begyndelsen af arrayet.

The array in the process:
Arrayet i processen skal se sådan ud:

```js no-beautify
Jazz, Blues
Expand Down
8 changes: 4 additions & 4 deletions 1-js/05-data-types/04-array/3-call-array-this/solution.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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`.
Kaldet `arr[2]()` er syntaktisk den gode gamle `obj[method]()`, hvor `arr` spiller rollen som `obj`, og `2` spiller rollen som `method`.

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:
Så vi har et kald af funktionen `arr[2]` som en objektmetode. Naturligvis modtager den `this`, der refererer til objektet `arr` og udskriver arrayet:

```js run
let arr = ["a", "b"];
Expand All @@ -10,6 +10,6 @@ arr.push(function() {
})

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

The array has 3 values: initially it had two, plus the function.
Arrayet har 3 værdier: oprindeligt havde det to, plus funktionen. Når vi kalder `arr[2]()`, så er `this` i funktionen lig med `arr`, og det udskrives som "a,b,function(){...}".
4 changes: 2 additions & 2 deletions 1-js/05-data-types/04-array/3-call-array-this/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# Calling in an array context
# Kald i et array-kontekst

What is the result? Why?
Hvad er resultatet? Hvorfor?

```js
let arr = ["a", "b"];
Expand Down
6 changes: 3 additions & 3 deletions 1-js/05-data-types/04-array/5-array-input-sum/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Please note the subtle, but important detail of the solution. We don't convert `value` to number instantly after `prompt`, because after `value = +value` we would not be able to tell an empty string (stop sign) from the zero (valid number). We do it later instead.
Bemærk den subtile, men vigtige detalje i løsningen. Vi konverterer ikke `value` til et tal med det samme efter `prompt`, fordi efter `value = +value` ville vi ikke kunne skelne en tom streng (stoptegn) fra nul (gyldigt tal). Vi gør det senere i stedet.


```js run demo
Expand All @@ -8,9 +8,9 @@ function sumInput() {

while (true) {

let value = prompt("A number please?", 0);
let value = prompt("Indtast et tal?", 0);

// should we cancel?
// skal vi afbryde?
if (value === "" || value === null || !isFinite(value)) break;

numbers.push(+value);
Expand Down
12 changes: 6 additions & 6 deletions 1-js/05-data-types/04-array/5-array-input-sum/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ importance: 4

---

# Sum input numbers
# Opsummer indtastede tal

Write the function `sumInput()` that:
Skriv en funktion `sumInput()` som:

- Asks the user for values using `prompt` and stores the values in the array.
- Finishes asking when the user enters a non-numeric value, an empty string, or presses "Cancel".
- Calculates and returns the sum of array items.
- Spørger brugeren om værdier med `prompt` og gemmer værdierne i et array.
- Stopper med at spørge, når brugeren indtaster en ikke-numerisk værdi, en tom streng eller trykker "Annuller".
- Beregner og returnerer summen af arrayets elementer.

P.S. A zero `0` is a valid number, please don't stop the input on zero.
P.S. Et nul `0` er et gyldigt tal, så stop ikke inputtet ved nul.

[demo]
Loading