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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Yes, it's possible.
Ja, det er muligt.

If a function returns an object then `new` returns it instead of `this`.
Hvis en funktion returnerer et objekt, så returnerer `new` det i stedet for `this`.

So they can, for instance, return the same externally defined object `obj`:
Så de kan for eksempel returnere det samme eksternt definerede objekt `obj`:

```js run no-beautify
let obj = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Two functionsone object
# To funktionerét objekt

Is it possible to create functions `A` and `B` so that `new A() == new B()`?
Er det muligt at skabe funktionerne `A` og `B`, så `new A() == new B()`?

```js no-beautify
function A() { ... }
Expand All @@ -16,4 +16,4 @@ let b = new B();
alert( a == b ); // true
```

If it is, then provide an example of their code.
Hvis det er tilfældet, så giv et eksempel på deres kode.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

describe("calculator", function() {
describe("lommeregner", function () {
let calculator;
before(function() {
before(function () {
sinon.stub(window, "prompt")

prompt.onCall(0).returns("2");
Expand All @@ -10,21 +10,21 @@ describe("calculator", function() {
calculator = new Calculator();
calculator.read();
});
it("the read method asks for two values using prompt and remembers them in object properties", function() {

it("read-metoden spørger efter to værdier ved hjælp af prompt og husker dem i objektets egenskaber", function () {
assert.equal(calculator.a, 2);
assert.equal(calculator.b, 3);
});

it("when 2 and 3 are entered, the sum is 5", function() {
it("når 2 og 3 indtastes, er summen 5", function () {
assert.equal(calculator.sum(), 5);
});

it("when 2 and 3 are entered, the product is 6", function() {
it("når 2 og 3 indtastes, er produktet 6", function () {
assert.equal(calculator.mul(), 6);
});

after(function() {
after(function () {
prompt.restore();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ importance: 5

---

# Create new Calculator
# Opret en ny lommeregner

Create a constructor function `Calculator` that creates objects with 3 methods:
Opret en konstruktørfunktion `Calculator`, der skaber objekter med 3 metoder:

- `read()` prompts for two values and saves them as object properties with names `a` and `b` respectively.
- `sum()` returns the sum of these properties.
- `mul()` returns the multiplication product of these properties.
- `read()` spørger efter to værdier og gemmer dem som egenskaber med navnene `a` og `b`.
- `sum()` returnerer summen af disse egenskaber.
- `mul()` returnerer produktet af disse egenskaber.

For instance:
For eksempel:

```js
let calculator = new Calculator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Hvor meget vil du tilføje?', 0);
};

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
describe("Accumulator", function() {
describe("Accumulator", function () {

beforeEach(function() {
beforeEach(function () {
sinon.stub(window, "prompt")
});

afterEach(function() {
afterEach(function () {
prompt.restore();
});

it("initial value is the argument of the constructor", function() {
it("initialiseret værdi er argumentet for konstruktøren", function () {
let accumulator = new Accumulator(1);

assert.equal(accumulator.value, 1);
});

it("after reading 0, the value is 1", function() {
it("efter at have læst 0, er værdien 1", function () {
let accumulator = new Accumulator(1);
prompt.returns("0");
accumulator.read();
assert.equal(accumulator.value, 1);
});

it("after reading 1, the value is 2", function() {
it("efter at have læst 1, er værdien 2", function () {
let accumulator = new Accumulator(1);
prompt.returns("1");
accumulator.read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function Accumulator(startingValue) {
this.value = startingValue;

this.read = function() {
this.value += +prompt('How much to add?', 0);
this.value += +prompt('Hvor meget vil du tilføje?', 0);
};

}
Expand Down
22 changes: 11 additions & 11 deletions 1-js/04-object-basics/06-constructor-new/3-accumulator/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ importance: 5

---

# Create new Accumulator
# Opret en akkumulator

Create a constructor function `Accumulator(startingValue)`.
Opret en konstruktørfunktion `Accumulator(startingValue)`.

Object that it creates should:
Objektet det opretter skal:

- Store the "current value" in the property `value`. The starting value is set to the argument of the constructor `startingValue`.
- The `read()` method should use `prompt` to read a new number and add it to `value`.
- Gemme den "nuværende værdi" i egenskaben `value`. Startværdien sættes til argumentet for konstruktøren `startingValue`.
- `read()`-metoden skal bruge `prompt` til at læse et nyt tal og lægge det til `value`.

In other words, the `value` property is the sum of all user-entered values with the initial value `startingValue`.
Med andre ord er `value`-egenskaben summen af alle brugerindtastede værdier med startværdien `startingValue`.

Here's the demo of the code:
Her er et eksempel på koden, der bruger `Accumulator`:

```js
let accumulator = new Accumulator(1); // initial value 1
let accumulator = new Accumulator(1); // Initialiser med værdien 1

accumulator.read(); // adds the user-entered value
accumulator.read(); // adds the user-entered value
accumulator.read(); // tilføjer den brugerindtastede værdi
accumulator.read(); // tilføjer den brugerindtastede værdi

alert(accumulator.value); // shows the sum of these values
alert(accumulator.value); // viser summen af disse værdier
```

[demo]
Loading