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 5fad2ff00a1bd4dad3c4b68cd0cd069696fa307a Mon Sep 17 00:00:00 2001 From: Sergey Melyukov Date: Sat, 2 Mar 2024 11:44:55 +0300 Subject: [PATCH 2/3] relax ts strictness --- tsconfig.json | 1 - 1 file changed, 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index c3de4b67b..e8a24b4ce 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,7 +11,6 @@ "esModuleInterop": true, "strict": true, "noImplicitAny": true, - "noImplicitReturns": true, "resolveJsonModule": true, "declaration": true, "incremental": true, From 657506831318627a9e80f7f225315a7a29fea626 Mon Sep 17 00:00:00 2001 From: JuckM4n Date: Thu, 21 Mar 2024 17:49:42 +0300 Subject: [PATCH 3/3] Week 1 is done --- projects/functions/index.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/projects/functions/index.js b/projects/functions/index.js index 38ebca33b..34aa59538 100644 --- a/projects/functions/index.js +++ b/projects/functions/index.js @@ -12,7 +12,9 @@ Другими словами: функция должна возвращать в неизменном виде то, что поступает ей на вход */ -function returnFirstArgument() {} +function returnFirstArgument(param) { + return param; +} /* Задание 2: @@ -28,7 +30,9 @@ function returnFirstArgument() {} Пример: sumWithDefaults(10) вернет 110 */ -function sumWithDefaults() {} +function sumWithDefaults(a, b = 100) { + return a + b; +} /* Задание 3: @@ -38,7 +42,9 @@ function sumWithDefaults() {} Пример: returnFnResult(() => 'привет') вернет 'привет' */ -function returnFnResult() {} +function returnFnResult(fn) { + return fn(); +} /* Задание 4: @@ -53,7 +59,11 @@ function returnFnResult() {} console.log(f()); // выведет 12 console.log(f()); // выведет 13 */ -function returnCounter() {} +function returnCounter(count = 0) { + return function () { + return ++count; + }; +} /* Задание 5 *: @@ -64,7 +74,9 @@ function returnCounter() {} Пример: returnArgumentsArray(1, 2, 3) вернет [1, 2, 3] */ -function returnArgumentsArray() {} +function returnArgumentsArray(...args) { + return args; +} export { returnFirstArgument,