From 8a50a6dc2d1461ca342a6df72926523735b7e9f4 Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Mon, 27 Feb 2023 11:57:55 +0300 Subject: [PATCH 1/3] week-1 --- projects/functions/index.js | 75 +++++++++++++++++++++++++++++++ projects/functions/index.spec.js | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 projects/functions/index.js create mode 100644 projects/functions/index.spec.js diff --git a/projects/functions/index.js b/projects/functions/index.js new file mode 100644 index 000000000..38ebca33b --- /dev/null +++ b/projects/functions/index.js @@ -0,0 +1,75 @@ +/* ДЗ 1 - Функции */ + +/* + Задание 1: + + 1.1: Добавьте к функции параметр с любым именем + 1.2: Функция должна возвращать аргумент, переданный в параметре + + Пример: + returnFirstArgument(10) вернет 10 + returnFirstArgument('привет') вернет `привет` + + Другими словами: функция должна возвращать в неизменном виде то, что поступает ей на вход + */ +function returnFirstArgument() {} + +/* + Задание 2: + + 2.1: Функция должна возвращать сумму переданных аргументов + + Пример: + sumWithDefaults(10, 20) вернет 30 + sumWithDefaults(2, 4) вернет 6 + + 2.2 *: Значение по умолчанию для второго аргумента должно быть равно 100 + + Пример: + sumWithDefaults(10) вернет 110 + */ +function sumWithDefaults() {} + +/* + Задание 3: + + Функция должна принимать другую функцию и возвращать результат вызова этой функции + + Пример: + returnFnResult(() => 'привет') вернет 'привет' + */ +function returnFnResult() {} + +/* + Задание 4: + + Функция должна принимать число и возвращать новую функцию (F) + При вызове функции F, переданное ранее число должно быть увеличено на единицу и возвращено из F + + Пример: + var f = returnCounter(10); + + console.log(f()); // выведет 11 + console.log(f()); // выведет 12 + console.log(f()); // выведет 13 + */ +function returnCounter() {} + +/* + Задание 5 *: + + Функция должна возвращать все переданные ей аргументы в виде массива + Количество переданных аргументов заранее неизвестно + + Пример: + returnArgumentsArray(1, 2, 3) вернет [1, 2, 3] + */ +function returnArgumentsArray() {} + +export { + returnFirstArgument, + sumWithDefaults, + returnArgumentsArray, + returnFnResult, + returnCounter, +}; diff --git a/projects/functions/index.spec.js b/projects/functions/index.spec.js new file mode 100644 index 000000000..396d4a58e --- /dev/null +++ b/projects/functions/index.spec.js @@ -0,0 +1,77 @@ +import { + returnArgumentsArray, + returnCounter, + returnFirstArgument, + returnFnResult, + sumWithDefaults, +} from './index'; + +describe('ДЗ 1 - функции', () => { + describe('returnFirstArgument', () => { + it('должна возвращать переданный аргумент', () => { + expect(returnFirstArgument(123)).toBe(123); + expect(returnFirstArgument('ls')).toBe('ls'); + }); + }); + + describe('sumWithDefaults', () => { + it('должна возвращать сумму переданных аргументов', () => { + expect(sumWithDefaults(1, 2)).toBe(3); + expect(sumWithDefaults(10, -2)).toBe(8); + }); + + it('значение по умолчанию второго аргумента должно быть 100', () => { + expect(sumWithDefaults(10)).toBe(110); + expect(sumWithDefaults(-2)).toBe(98); + }); + }); + + describe('returnFnResult', () => { + it('должна возвращать результат вызова переданной функции', () => { + function fn() { + return value; + } + + const value = Math.random(); + const result = returnFnResult(fn); // result = fn() -> value + + expect(result).toBe(value); + }); + }); + + describe('returnCounter', () => { + it('должна возвращать функцию', () => { + const result = returnCounter(); + + expect(typeof result).toBe('function'); + }); + + it('возвращаемая функция должна увеличивать переданное число на единицу при каждом вызове', () => { + const value = parseInt(Math.random() * 10, 10); + const result = returnCounter(value); + + expect(result()).toBe(value + 1); + expect(result()).toBe(value + 2); + expect(result()).toBe(value + 3); + }); + + it('значение аргумента должно быть 0 по умолчанию', () => { + const result = returnCounter(); + + expect(result()).toBe(1); + expect(result()).toBe(2); + expect(result()).toBe(3); + }); + }); + + describe('returnArgumentsArray', () => { + it('должна возвращать переданные аргументы в виде массива', () => { + expect(returnArgumentsArray(1, 2, 3)).toEqual([1, 2, 3]); + expect(returnArgumentsArray('l', 's')).toEqual(['l', 's']); + }); + + it('должна возвращать пустой массив если нет аргументов', () => { + expect(returnArgumentsArray()).toEqual([]); + }); + }); +}); From b78244d4d50ad344ce49e0d0a5ef4f487dfcfd80 Mon Sep 17 00:00:00 2001 From: Darya Kolesnikova Date: Sat, 10 Jun 2023 10:11:43 +0500 Subject: [PATCH 2/3] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=B4=D0=B71?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projects/functions/index.js | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/projects/functions/index.js b/projects/functions/index.js index 38ebca33b..3326ec1b0 100644 --- a/projects/functions/index.js +++ b/projects/functions/index.js @@ -12,7 +12,9 @@ Другими словами: функция должна возвращать в неизменном виде то, что поступает ей на вход */ -function returnFirstArgument() {} +function returnFirstArgument(foo) { + return foo; +} /* Задание 2: @@ -28,8 +30,13 @@ function returnFirstArgument() {} Пример: sumWithDefaults(10) вернет 110 */ -function sumWithDefaults() {} - +function sumWithDefaults(a, b) { + return a + b; +} +function sumWithDefaults_2(a) { + const b = 100; + return a + b; +} /* Задание 3: @@ -38,7 +45,9 @@ function sumWithDefaults() {} Пример: returnFnResult(() => 'привет') вернет 'привет' */ -function returnFnResult() {} +function returnFnResult(fn) { + return fn(); +} /* Задание 4: @@ -53,7 +62,9 @@ function returnFnResult() {} console.log(f()); // выведет 12 console.log(f()); // выведет 13 */ -function returnCounter() {} +function returnCounter(foo) { + return () => {return ++foo;} +} /* Задание 5 *: @@ -64,7 +75,9 @@ function returnCounter() {} Пример: returnArgumentsArray(1, 2, 3) вернет [1, 2, 3] */ -function returnArgumentsArray() {} +function returnArgumentsArray() { + return [...arguments]; +} export { returnFirstArgument, From 87bd03e3ef992c642ff23aab772d31195f250467 Mon Sep 17 00:00:00 2001 From: Darya Kolesnikova Date: Sun, 11 Jun 2023 11:54:53 +0500 Subject: [PATCH 3/3] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D1=8E=20?= =?UTF-8?q?=D1=81=D1=83=D0=BC=D0=BC=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- projects/functions/index.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/projects/functions/index.js b/projects/functions/index.js index 3326ec1b0..af256fc32 100644 --- a/projects/functions/index.js +++ b/projects/functions/index.js @@ -30,11 +30,7 @@ function returnFirstArgument(foo) { Пример: sumWithDefaults(10) вернет 110 */ -function sumWithDefaults(a, b) { - return a + b; -} -function sumWithDefaults_2(a) { - const b = 100; +function sumWithDefaults(a, b = 100) { return a + b; } /*