Skip to content

Lesson 6 #79

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: lesson-6
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions lesson-6/JS/task1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Задание 1
* Реализовать функцию, которая суммирует аргументы.
*
* function sum(x) {
* ...
* }
*
* sum(1)(2)(3) === 6
*/

function sum(firstarg) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

удали строку

let curry = (nextarg) => {
firstarg += nextarg;
return curry;
}

curry.toString = () => firstarg;

return curry;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof sum(1)(2)(3) = "function", а должно быть number

console.log('Задание 1');
console.log( sum(1)(2)(3) );
32 changes: 32 additions & 0 deletions lesson-6/JS/task2.js
Original file line number Diff line number Diff line change
@@ -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));
38 changes: 38 additions & 0 deletions lesson-6/JS/task3.js
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lesson-6/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JS-courses</title>
</head>
<body>
<h1>lesson 6<h1>
<script defer src="JS/task1.js"></script>
<script defer src="JS/task2.js"></script>
<script defer src="JS/task3.js"></script>
</body>
</html>