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

Function object, NFE#168

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 2 commits intojavascript-tutorial:masterfromotmon76:1.6.6
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
functionmakeCounter() {
letcount = 0;
functionvytvořČítač() {
letpočet = 0;

functioncounter() {
returncount++;
functiončítač() {
returnpočet++;
}

counter.set =value =>count =value;
čítač.nastav =hodnota =>počet =hodnota;

counter.decrease = () =>count--;
čítač.sniž = () =>počet--;

returncounter;
returnčítač;
}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
functionmakeCounter() {
letcount = 0;
functionvytvořČítač() {
letpočet = 0;

// ...your code ...
// ...váš kód ...
}

letcounter =makeCounter();
letčítač =vytvořČítač();

alert(counter() ); // 0
alert(counter() ); // 1
alert(čítač() ); // 0
alert(čítač() ); // 1

counter.set(10); //set the new count
čítač.nastav(10); //nastaví nový počet

alert(counter() ); // 10
alert(čítač() ); // 10

counter.decrease(); //decrease the count by 1
čítač.sniž(); //sníží počet o 1

alert(counter() ); // 10 (instead of 11)
alert(čítač() ); // 10 (místo 11)
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
describe("counter", function() {
describe("čítač", function() {

it("increases from call to call", function() {
it("zvýší se při každém volání", function() {

letcounter =makeCounter();
letčítač =vytvořČítač();

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


describe("counter.set", function() {
it("sets the count", function() {
describe("čítač.nastav", function() {
it("nastaví počet", function() {

letcounter =makeCounter();
letčítač =vytvořČítač();

counter.set(10);
čítač.nastav(10);

assert.equal(counter(), 10 );
assert.equal(counter(), 11 );
assert.equal(čítač(), 10 );
assert.equal(čítač(), 11 );
});
});

describe("counter.decrease", function() {
it("decreases the count", function() {
describe("čítač.sniž", function() {
it("sníží počet", function() {

letcounter =makeCounter();
letčítač =vytvořČítač();

counter.set(10);
čítač.nastav(10);

assert.equal(counter(), 10 );
assert.equal(čítač(), 10 );

counter.decrease();
čítač.sniž();

assert.equal(counter(), 10 );
assert.equal(čítač(), 10 );

});
});
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff 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`.
Řešení využívá `počet` v lokální proměnné, ale přidané metody jsou vepsány přímo do funkce `čítač`.Sdílejí stejné vnější lexikální prostředí a mohou také přistupovat k aktuální hodnotě proměnné `počet`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,14 +2,14 @@ importance: 5

---

#Set and decrease for counter
#Nastavení a snížení čítače

Modify the code of `makeCounter()`so that the counter can also decrease and set the number:
Upravte kód funkce `vytvořČítač()`tak, aby tento čítač uměl také snížit a nastavit svou hodnotu:

- `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.
- `čítač()`by měl vracet další číslo (jako dříve).
- `čítač.nastav(hodnota)`by měl nastavit čítač na hodnotu `hodnota`.
- `čítač.sniž()`by měl snížit čítač o 1.

See the sandbox code for the complete usage example.
Úplný příklad použití uvidíte na kódu z pískoviště.

P.S.You can use either a closure or the function property to keep the current count. Or write both variants.
P.S.K uchovávání aktuálního počtu můžete využívat buď uzávěr, nebo vlastnost funkce. Nebo napsat obě varianty.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
functionsum(a) {
functionsečti(a) {

letcurrentSum = a;
letaktuálníSoučet = a;

function f(b) {
currentSum += b;
aktuálníSoučet += b;
return f;
}

f.toString = function() {
returncurrentSum;
returnaktuálníSoučet;
};

return f;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
functionsum(a){
//Your code goes here.
functionsečti(a){
//Sem přijde váš kód.

}

/*
sum(1)(2) == 3; // 1 + 2
sum(1)(2)(3) == 6; // 1 + 2 + 3
sum(5)(-1)(2) == 6
sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15
sečti(1)(2) == 3; // 1 + 2
sečti(1)(2)(3) == 6; // 1 + 2 + 3
sečti(5)(-1)(2) == 6
sečti(6)(-1)(-2)(-3) == 0
sečti(0)(1)(2)(3)(4)(5) == 15
*/
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
describe("sum", function(){
describe("sečti", function(){

it("sum(1)(2) == 3", function(){
assert.equal(3,sum(1)(2));
it("sečti(1)(2) == 3", function(){
assert.equal(3,sečti(1)(2));
});

it("sum(5)(-1)(2) == 6", function(){
assert.equal(6,sum(5)(-1)(2));
it("sečti(5)(-1)(2) == 6", function(){
assert.equal(6,sečti(5)(-1)(2));
});

it("sum(6)(-1)(-2)(-3) == 0", function(){
assert.equal(0,sum(6)(-1)(-2)(-3));
it("sečti(6)(-1)(-2)(-3) == 0", function(){
assert.equal(0,sečti(6)(-1)(-2)(-3));
});

it("sum(0)(1)(2)(3)(4)(5) == 15", function(){
assert.equal(15,sum(0)(1)(2)(3)(4)(5));
it("sečti(0)(1)(2)(3)(4)(5) == 15", function(){
assert.equal(15,sečti(0)(1)(2)(3)(4)(5));
});
});

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@

1.For the whole thingtowork *anyhow*, the result of `sum` must be function.
2.That function must keep in memory the current value between calls.
3.According to the task, the function must become the number when used in`==`.Functions are objects, so the conversion happens as described in the chapter<info:object-toprimitive>,and we can provide our own method that returns the number.
1.Abytocelé *jakkoli* fungovalo, výsledek funkce `sečti` musí být funkce.
2.Tato funkce si musí udržovat v paměti aktuální hodnotu mezi voláními.
3.Podle zadání se funkce musí stát číslem, když je použita v`==`.Funkce jsou objekty, takže konverze se odehrává tak, jak je popsáno v kapitole<info:object-toprimitive>,a my můžeme poskytnout svou vlastní metodu, která toto číslo vrátí.

Now the code:
Nyní kód:

```js demo run
functionsum(a) {
functionsečti(a) {

letcurrentSum = a;
letaktuálníSoučet = a;

function f(b) {
currentSum += b;
aktuálníSoučet += b;
return f;
}

f.toString = function() {
returncurrentSum;
returnaktuálníSoučet;
};

return f;
}

alert(sum(1)(2) ); // 3
alert(sum(5)(-1)(2) ); // 6
alert(sum(6)(-1)(-2)(-3) ); // 0
alert(sum(0)(1)(2)(3)(4)(5) ); // 15
alert(sečti(1)(2) ); // 3
alert(sečti(5)(-1)(2) ); // 6
alert(sečti(6)(-1)(-2)(-3) ); // 0
alert(sečti(0)(1)(2)(3)(4)(5) ); // 15
```

Please note that the `sum` function actually works only once. It returns function `f`.
Prosíme všimněte si, že funkce `sečti` se ve skutečnosti spustí jenom jednou. Vrátí funkci `f`.

Then, on each subsequent call,`f`adds its parameter to the sum `currentSum`, and returns itself.
Pak funkce`f`při každém následném volání přičte svůj parametr k součtu `aktuálníSoučet` a vrátí sebe sama.

**There is no recursion in the last line of`f`.**
**Na posledním řádku funkce`f` není rekurze.**

Here is what recursion looks like:
Rekurze by vypadala takto:

```js
function f(b) {
currentSum += b;
return f(); // <--recursive call
aktuálníSoučet += b;
return f(); // <--rekurzívní volání
}
```

And in our case, we just return the function, without calling it:
V našem případě vracíme jen funkci, aniž bychom ji volali:

```js
function f(b) {
currentSum += b;
return f; // <--does not call itself, returns itself
aktuálníSoučet += b;
return f; // <--nevolá sama sebe, vrací sama sebe
}
```

This`f`will be used in the next call, again return itself, as many times as needed. Then, when used as a number or a string -- the`toString`returns the `currentSum`.We could also use`Symbol.toPrimitive`or `valueOf` here for the conversion.
Tato funkce`f`bude použita při dalším volání a opět vrátí sebe sama, tolikrát, kolikrát je zapotřebí. Když ji pak použijeme jako číslo nebo řetězec,`toString`vrátí `aktuálníSoučet`.Zde bychom pro konverzi mohli také použít`Symbol.toPrimitive`nebo `valueOf`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,16 +2,16 @@ importance: 2

---

#Sum with an arbitrary amount of brackets
#Sčítání s libovolným počtem závorek

Write function `sum` that would work like this:
Napište funkci `sečti`, která bude fungovat takto:

```js
sum(1)(2) == 3; // 1 + 2
sum(1)(2)(3) == 6; // 1 + 2 + 3
sum(5)(-1)(2) == 6
sum(6)(-1)(-2)(-3) == 0
sum(0)(1)(2)(3)(4)(5) == 15
sečti(1)(2) == 3; // 1 + 2
sečti(1)(2)(3) == 6; // 1 + 2 + 3
sečti(5)(-1)(2) == 6
sečti(6)(-1)(-2)(-3) == 0
sečti(0)(1)(2)(3)(4)(5) == 15
```

P.S.Hint: you may need to setup custom object to primitive conversion for your function.
P.S.Rada: možná budete pro svou funkci potřebovat nastavit vlastní konverzi objektu na primitiv.
Loading

[8]ページ先頭

©2009-2025 Movatter.jp