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 ee652a8c805c67cc5a38faf9d7d539076e308d0c Mon Sep 17 00:00:00 2001 From: lFrodo Date: Wed, 19 Apr 2023 20:00:56 +0300 Subject: [PATCH 2/3] 1st --- projects/functions/index.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/projects/functions/index.js b/projects/functions/index.js index 38ebca33b..20e4f7ae9 100644 --- a/projects/functions/index.js +++ b/projects/functions/index.js @@ -12,8 +12,13 @@ Другими словами: функция должна возвращать в неизменном виде то, что поступает ей на вход */ -function returnFirstArgument() {} - +function returnFirstArgument(a) { + if (a === typeof 'number') { + return (a = Number()); + } + return a; +} +returnFirstArgument('ls'); /* Задание 2: @@ -28,7 +33,10 @@ function returnFirstArgument() {} Пример: sumWithDefaults(10) вернет 110 */ -function sumWithDefaults() {} +function sumWithDefaults(a, b = 100) { + return a + b; +} +sumWithDefaults(10, 20); /* Задание 3: @@ -38,7 +46,9 @@ function sumWithDefaults() {} Пример: returnFnResult(() => 'привет') вернет 'привет' */ -function returnFnResult() {} +function returnFnResult(a) { + return a(); +} /* Задание 4: @@ -53,7 +63,14 @@ function returnFnResult() {} console.log(f()); // выведет 12 console.log(f()); // выведет 13 */ -function returnCounter() {} + +function returnCounter(count = 0) { + + return function F() { + return count = count + 1; + }; + +} /* Задание 5 *: @@ -64,7 +81,9 @@ function returnCounter() {} Пример: returnArgumentsArray(1, 2, 3) вернет [1, 2, 3] */ -function returnArgumentsArray() {} +function returnArgumentsArray(...args) { + return args +} export { returnFirstArgument, From f275b0e682e01084eb0ceceeff3d5adbf2005378 Mon Sep 17 00:00:00 2001 From: Igor Pshenichnyy <76572688+iFrodo@users.noreply.github.com> Date: Fri, 21 Apr 2023 08:00:19 +0300 Subject: [PATCH 3/3] corrections --- projects/functions/index.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/projects/functions/index.js b/projects/functions/index.js index 20e4f7ae9..3fa601eac 100644 --- a/projects/functions/index.js +++ b/projects/functions/index.js @@ -13,9 +13,6 @@ Другими словами: функция должна возвращать в неизменном виде то, что поступает ей на вход */ function returnFirstArgument(a) { - if (a === typeof 'number') { - return (a = Number()); - } return a; } returnFirstArgument('ls');