diff --git a/lesson-6/JS/task1.js b/lesson-6/JS/task1.js new file mode 100644 index 0000000..fc5bf8c --- /dev/null +++ b/lesson-6/JS/task1.js @@ -0,0 +1,25 @@ +/* +* Задание 1 +* Реализовать функцию, которая суммирует аргументы. +* +* function sum(x) { +* ... +* } +* +* sum(1)(2)(3) === 6 +*/ + +function sum(firstarg) { + + let curry = (nextarg) => { + firstarg += nextarg; + return curry; + } + + curry.toString = () => firstarg; + + return curry; +} + +console.log('Задание 1'); +console.log( sum(1)(2)(3) ); \ No newline at end of file diff --git a/lesson-6/JS/task2.js b/lesson-6/JS/task2.js new file mode 100644 index 0000000..0b225bd --- /dev/null +++ b/lesson-6/JS/task2.js @@ -0,0 +1,32 @@ +/* +* Задание 2 +* Реализовать функцию, которая суммирует аргументы, если аргумент не передан - вернуть сумму. +* +* function sum(x) { +* ... +* } +* +* sum(1)(2)(3)...(N)() === сумме всех чисел до N +* +* +* Реализовать эту же функцию, только с возможностью получения значения без дополнительного пустого вызова. +* +* alert(sum(1)(2)(3)...(N)) +*/ + +// Задание 2.1 +function sum(arg) { + return nextArg => nextArg ? sum(arg + nextArg) : arg; +} + +console.log('\nЗадание 2.1'); +console.log( sum(1)(2)(3)(4)(5)(6)() ); + +// Задание 2.2 +function sumWOEmpty(arg) { + const add = nextArg => sumWOEmpty(arg + nextArg); + add.toString = () => arg; + return add; +} + +alert(sumWOEmpty(1)(2)(3)(4)(5)(6)); \ No newline at end of file diff --git a/lesson-6/JS/task3.js b/lesson-6/JS/task3.js new file mode 100644 index 0000000..f26c943 --- /dev/null +++ b/lesson-6/JS/task3.js @@ -0,0 +1,38 @@ +/* +* Задание 3 +* Реализовать счетчик который при вызове должен возвращать число на 1 больше, +* также иметь методы set и reset, работать это должно следующим образом. +* +* const counter = makeCounter(); +* counter() // 1 +* counter() // 2 +* counter.set(12); +* counter() // 12 +* counter.reset(); +* counter() // 1 +*/ + +function makeCounter() { + let initialValue = 0; + + const counter = () => ++initialValue; + + counter.set = (value) => { + initialValue = --value; + }; + + counter.reset = () => { + initialValue = 0; + }; + + return counter; +} + +console.log('\nЗадание 3'); +const counter = makeCounter(); +console.log(counter()); // 1 +console.log(counter()); // 2 +counter.set(12); +console.log(counter()); // 12 +counter.reset(); +console.log(counter()); // 1 \ No newline at end of file diff --git a/lesson-6/index.html b/lesson-6/index.html new file mode 100644 index 0000000..7250cd3 --- /dev/null +++ b/lesson-6/index.html @@ -0,0 +1,13 @@ + + + + + JS-courses + + +

lesson 6

+ + + + + \ No newline at end of file