From f015cbc8855e28eac26e64fed14a07293cc65490 Mon Sep 17 00:00:00 2001 From: osvetilka Date: Mon, 3 Feb 2025 01:25:14 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=203.1=20?= =?UTF-8?q?=D0=BD=D0=B5=20=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BD=D0=B0?= =?UTF-8?q?,=20=D0=BD=D0=B5=20=D0=BF=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D0=B8?= =?UTF-8?q?=D1=82=201=20=D1=82=D0=B5=D1=81=D1=82.=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B8=203.2-3.3=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1-create-getter/index.js | 8 +++++++- .../2-invert-object/index.js | 4 ++++ .../3-trim-symbols/index.js | 15 +++++++++++++++ .../4-uniq/index.js | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/03-objects-arrays-intro-to-testing/1-create-getter/index.js b/03-objects-arrays-intro-to-testing/1-create-getter/index.js index c86ac40..9e9ac0c 100644 --- a/03-objects-arrays-intro-to-testing/1-create-getter/index.js +++ b/03-objects-arrays-intro-to-testing/1-create-getter/index.js @@ -4,5 +4,11 @@ * @returns {function} - function-getter which allow get value from object by set path */ export function createGetter(path) { - + const arr = path.split('.'); + + return function(obj) { + return arr.reduce((accum, item)=> + accum.hasOwnProperty(item) ? accum[item] : undefined, obj) + + } } diff --git a/03-objects-arrays-intro-to-testing/2-invert-object/index.js b/03-objects-arrays-intro-to-testing/2-invert-object/index.js index d6f6b7c..8ed6089 100644 --- a/03-objects-arrays-intro-to-testing/2-invert-object/index.js +++ b/03-objects-arrays-intro-to-testing/2-invert-object/index.js @@ -4,5 +4,9 @@ * @returns {object | undefined} - returns new object or undefined if nothing did't pass */ export function invertObj(obj) { + if (obj === undefined) + return; + const arr = Object.entries(obj); + return Object.fromEntries(arr.map(([keys,values])=>[values,keys])); } diff --git a/03-objects-arrays-intro-to-testing/3-trim-symbols/index.js b/03-objects-arrays-intro-to-testing/3-trim-symbols/index.js index bdafd72..e08ba6a 100644 --- a/03-objects-arrays-intro-to-testing/3-trim-symbols/index.js +++ b/03-objects-arrays-intro-to-testing/3-trim-symbols/index.js @@ -5,5 +5,20 @@ * @returns {string} - the new string without extra symbols according passed size */ export function trimSymbols(string, size) { + if (string==='' || size === 0) return ''; + if (size === undefined) return string; + let newStr = ''; + let n = 1; + for (let i = 0; i < string.length; i++) { + if (string[i] === string[i-1]) { + n++; + } + else n = 1; + + if (n <= size) { + newStr = newStr + string[i]; + } + } + return newStr; } diff --git a/03-objects-arrays-intro-to-testing/4-uniq/index.js b/03-objects-arrays-intro-to-testing/4-uniq/index.js index b81423b..fa6cce3 100644 --- a/03-objects-arrays-intro-to-testing/4-uniq/index.js +++ b/03-objects-arrays-intro-to-testing/4-uniq/index.js @@ -4,5 +4,5 @@ * @returns {*[]} - the new array with uniq values */ export function uniq(arr) { - + return Array.from(new Set(arr)); }