FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Translated assignments to function objects by ockley · Pull Request #251 · javascript-tutorial/da.javascript.info · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .js  (2) .md  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
      • source.js
      • test.js
    • solution.md
    • 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
@@ -1,18 +1,18 @@
function makeCounter() {
let count = 0;

// ... your code ...
// ... din kode ...
}

let counter = makeCounter();

alert( counter() ); // 0
alert( counter() ); // 1
alert(counter()); // 0
alert(counter()); // 1

counter.set(10); // set the new count
counter.set(10); // sæt en ny værdi

alert( counter() ); // 10
alert(counter()); // 10

counter.decrease(); // decrease the count by 1
counter.decrease(); // formindsk tælleren med 1

alert( counter() ); // 10 (instead of 11)
alert(counter()); // 10 (i stedet for 11)
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 @@
describe("counter", function() {
describe("counter", function () {

it("increases from call to call", function() {
it("forøger tælleren fra kald til kald", function () {

let counter = makeCounter();

assert.equal( counter(), 0 );
assert.equal( counter(), 1 );
assert.equal( counter(), 2 );
assert.equal(counter(), 0);
assert.equal(counter(), 1);
assert.equal(counter(), 2);
});

describe("counter.set", function() {
it("sets the count", function() {

describe("counter.set", function () {
it("sætter tælleren", function () {

let counter = makeCounter();

counter.set(10);

assert.equal( counter(), 10 );
assert.equal( counter(), 11 );
assert.equal(counter(), 10);
assert.equal(counter(), 11);
});
});
describe("counter.decrease", function() {
it("decreases the count", function() {

describe("counter.decrease", function () {
it("formindsker tælleren", function () {

let counter = makeCounter();

counter.set(10);

assert.equal( counter(), 10 );
assert.equal(counter(), 10);

counter.decrease();

assert.equal( counter(), 10 );
assert.equal(counter(), 10);

});
});
Expand Down
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,2 +1,2 @@

The solution uses `count` in the local variable, but addition methods are written right into the `counter`. They share the same outer lexical environment and also can access the current `count`.
Løsningen bruger `count` i den lokale variabel, men tilføjede metoder er skrevet direkte ind i `counter`. De deler det samme ydre leksikale miljø og kan også tilgå den nuværende `count`.
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
Expand Up @@ -2,14 +2,14 @@ importance: 5

---

# Set and decrease for counter
# Sæt og formindsk for counter

Modify the code of `makeCounter()` so that the counter can also decrease and set the number:
Juster på koden `makeCounter()` så tælleren også kan formindske tallet og sætte den til et bestemt tal:

- `counter()` should return the next number (as before).
- `counter.set(value)` should set the counter to `value`.
- `counter.decrease()` should decrease the counter by 1.
- `counter()` skal returnere det næste tal (as before).
- `counter.set(value)` skal sætte tælleren til `value`.
- `counter.decrease()` skal formindske tælleren med 1.

See the sandbox code for the complete usage example.
Se sandbox-koden for det fulde brugseksempel.

P.S. You can use either a closure or the function property to keep the current count. Or write both variants.
P.S. Du kan bruge enten en closure eller funktionsegenskab til at gemme den nuværende tæller. Eller skriv begge varianter.

Back | FazBrowse Home | New Git URL