From 8225e82287c52bc1b0d01ea994db5337884ca6db Mon Sep 17 00:00:00 2001 From: Olesia Serban Date: Thu, 8 Jul 2021 16:44:50 +0300 Subject: [PATCH 1/4] changeCase, filterNonUnique, alphabetSort, getSecondMinimum, doubleEveryEven, getArrayElementsPairs --- modules/1_basics/index.js | 72 +++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/modules/1_basics/index.js b/modules/1_basics/index.js index cb5ff10..755585a 100644 --- a/modules/1_basics/index.js +++ b/modules/1_basics/index.js @@ -1,32 +1,92 @@ /** * Change the capitalization of all letters in a string */ -export const changeCase = () => {}; +export const changeCase = (str) => { + let result = ''; + for(let i = 0; i < str.length; i++) { + let char = str.charAt(i); + if(char === char.toUpperCase()){ + char = char.toLowerCase(); + }else if(char === char.toLowerCase()){ + char = char.toUpperCase(); + } + result = result + char; + } + return result; +}; /** * Filter out the non-unique values in an array */ -export const filterNonUnique = () => {}; +export const filterNonUnique = (arr) => { + let result = []; + for (const element of arr) { + let elmentCounter = 0; + for(const el2 of arr) { + if (element === el2){ + elmentCounter++; + } + } + if(elmentCounter === 1) { + result.push(element); + } + } + return result; +}; /** * Sort string in alphabetical order */ -export const alphabetSort = () => {}; +export const alphabetSort = (str) => { + let chars = str.split(''); + let sortedChars = chars.sort(); + return sortedChars.join(''); +}; /** * Get min integer */ -export const getSecondMinimum = () => {}; +export const getSecondMinimum = (arr) => { + let min = null; + let secMin = null; + arr = arr.sort(); + for (const el of arr){ + if (min === null || el <= min){ + min = el; + } else if (secMin === null || el <= secMin) { + secMin = el; + } + } + return secMin != null ? secMin : min; +}; /** * Double every even integer */ -export const doubleEveryEven = () => {}; +export const doubleEveryEven = (arr) => { + let result = []; + for(const element of arr) { + if(element % 2 === 0){ + result.push(element * 2); + }else{ + result.push(element); + } + } + return result; +}; /** * Create array with all possible pairs of two arrays */ -export const getArrayElementsPairs = () => {}; +export const getArrayElementsPairs = (arr1, arr2) => { + let result = []; + for(const el1 of arr1){ + for (const el2 of arr2){ + result.push([el1, el2]); + } + } + return result; +}; /** * Deep equal From 9691d05d848b2bf291c19abf8f499e4e6ba5798e Mon Sep 17 00:00:00 2001 From: Olesia Serban Date: Thu, 8 Jul 2021 20:51:32 +0300 Subject: [PATCH 2/4] formatDate --- modules/1_basics/index.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/1_basics/index.js b/modules/1_basics/index.js index 755585a..9211481 100644 --- a/modules/1_basics/index.js +++ b/modules/1_basics/index.js @@ -96,4 +96,19 @@ export const deepEqual = () => {}; /** * Format date */ -export const formatDate = () => {}; +export const formatDate = (date) => { + if (Array.isArray(date)){ + date = new Date(date[0], date[1], date[2]); + } else { + date = new Date(date); + } + + let day = date.getDate() + ''; + day = day.padStart(2, '0'); + let month = date.getMonth() + 1 + ''; + month = month.padStart(2,'0'); + let year = date.getFullYear() + ''; + year = year.slice(-2); + let result = day + "." + month + "." + year; + return result; +}; From 94be2aaffb676eb9b0d531e9e28cf1fb0d41e6cc Mon Sep 17 00:00:00 2001 From: Olesia Serban Date: Thu, 8 Jul 2021 21:34:40 +0300 Subject: [PATCH 3/4] deepEqual --- modules/1_basics/index.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/1_basics/index.js b/modules/1_basics/index.js index 9211481..968d977 100644 --- a/modules/1_basics/index.js +++ b/modules/1_basics/index.js @@ -91,7 +91,31 @@ export const getArrayElementsPairs = (arr1, arr2) => { /** * Deep equal */ -export const deepEqual = () => {}; +export const deepEqual = (o1, o2) => { + function isObject(obj) { + return obj === Object(obj); + } + + if (o1 === o2) { + return true; + } + + for(var p in o1){ + if(!o2.hasOwnProperty(p)) { // o2 does not have such property + return false; + } + if(!isObject(o1[p]) && o1[p]!== o2[p]) { // is simple type and not equal to o2 + return false; + } else if (isObject(o1[p]) && !isObject(o2[p])) { // o2 property is not complex type + return false; + } else if (isObject(o1[p]) && isObject(o2[p])){ // both are complex types + if(!deepEqual(o1[p], o2[p])) { // recursion check nested property + return false; + } + } + } + return true; // has not failed the previous steps +}; /** * Format date From db4e05574791d88f4d69096ffb6711fc47a9890c Mon Sep 17 00:00:00 2001 From: Olesia Serban Date: Thu, 8 Jul 2021 22:29:00 +0300 Subject: [PATCH 4/4] changing to str interpolation --- modules/1_basics/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/1_basics/index.js b/modules/1_basics/index.js index 968d977..2bcd350 100644 --- a/modules/1_basics/index.js +++ b/modules/1_basics/index.js @@ -133,6 +133,6 @@ export const formatDate = (date) => { month = month.padStart(2,'0'); let year = date.getFullYear() + ''; year = year.slice(-2); - let result = day + "." + month + "." + year; + let result = `${day}.${month}.${year}`; return result; };