Skip to content

Commit

Permalink
refactor: improve 03-arrays-tasks solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-koran committed Dec 10, 2024
1 parent 40ee498 commit 01abbed
Showing 1 changed file with 42 additions and 39 deletions.
81 changes: 42 additions & 39 deletions src/03-arrays-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,8 @@ function findElement(arr, value) {
* 2 => [ 1, 3 ]
* 5 => [ 1, 3, 5, 7, 9 ]
*/
function generateOdds(len) {
let temp = -1;
const result = new Array(len).fill(0);

return result.map(() => {
temp += 2;

return temp;
});
function generateOdds(length) {
return Array.from({ length }).map((_, index) => 2 * (index + 1) - 1);
}

/**
Expand Down Expand Up @@ -105,7 +98,7 @@ function getArrayOfStrings(arr) {
* [ false, 0, NaN, '', undefined ] => [ ]
*/
function removeFalsyValues(arr) {
return arr.filter((item) => !!item === true);
return arr.filter((el) => el);
}

/**
Expand Down Expand Up @@ -134,7 +127,7 @@ function getUpperCaseStrings(arr) {
* [ 'angular', 'react', 'ember' ] => [ 7, 5, 5 ]
*/
function getStringsLength(arr) {
return arr.map((item) => item.length);
return arr.map((el) => el.length);
}

/**
Expand Down Expand Up @@ -177,6 +170,10 @@ function getHead(arr, n) {
* [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ]
*/
function getTail(arr, n) {
if (n === 0) {
return [];
}

return arr.slice(-n);
}

Expand Down Expand Up @@ -275,7 +272,13 @@ function getSecondItems(arr) {
* [ 1,2,3,4,5 ] => [ 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5 ]
*/
function propagateItemsByPositionIndex(arr) {
return arr.reduce((acc, curr, index) => acc.concat(new Array(index + 1).fill(curr)), []);
return arr.reduce(
(acc, curr, index) => [
...acc,
...Array.from({ length: index + 1 }).fill(curr),
],
[]

Check failure on line 280 in src/03-arrays-tasks.js

View workflow job for this annotation

GitHub Actions / core-js-101

Missing trailing comma
);
}

/**
Expand Down Expand Up @@ -326,7 +329,8 @@ function getPositivesCount(arr) {
* [ 'one','one','one','zero' ] => [ 'zero','one','one','one' ]
*/
function sortDigitNamesByNumericOrder(arr) {
const strToIndex = {
const digitNamesSpecificity = {
zero: 0,
one: 1,
two: 2,
three: 3,
Expand All @@ -336,12 +340,12 @@ function sortDigitNamesByNumericOrder(arr) {
seven: 7,
eight: 8,
nine: 9,
zero: 0,
};

return arr.sort((a, b) => strToIndex[a] - strToIndex[b]);
return [...arr].sort(
(a, b) => digitNamesSpecificity[a] - digitNamesSpecificity[b]

Check failure on line 346 in src/03-arrays-tasks.js

View workflow job for this annotation

GitHub Actions / core-js-101

Missing trailing comma
);
}

/**
* Returns the sum of all items in the specified array of numbers
*
Expand Down Expand Up @@ -371,7 +375,7 @@ function getItemsSum(arr) {
* [ null, undefined, NaN, false, 0, '' ] => 6
*/
function getFalsyValuesCount(arr) {
return arr.filter((item) => !!item === false).length;
return arr.filter((el) => !el).length;
}

/**
Expand All @@ -389,7 +393,7 @@ function getFalsyValuesCount(arr) {
* [ true, 0, 1, 'true' ], true => 1
*/
function findAllOccurrences(arr, item) {
return arr.filter((arrItem) => arrItem === item).length;
return arr.filter((el) => el === item).length;
}

/**
Expand Down Expand Up @@ -476,16 +480,14 @@ function sortCitiesArray(arr) {
* [0,0,0,0,1]]
*/
function getIdentityMatrix(n) {
const result = new Array(n).fill([]).map(() => new Array(n).fill(0));

return result.map(
(firstArrayItem, firstArrayIndex) => (
firstArrayItem.map(
(secondArrayItem, secondArrayIndex) => (
firstArrayIndex === secondArrayIndex ? 1 : secondArrayItem
),
)
),
return Array.from({ length: n }).map((_column, columnIndex) =>
Array.from({ length: n }).map((_cell, cellIndex) => {

Check failure on line 484 in src/03-arrays-tasks.js

View workflow job for this annotation

GitHub Actions / core-js-101

Expected no linebreak before this expression
if (cellIndex === columnIndex) {
return 1;
}

return 0;
})

Check failure on line 490 in src/03-arrays-tasks.js

View workflow job for this annotation

GitHub Actions / core-js-101

Missing trailing comma
);

Check failure on line 491 in src/03-arrays-tasks.js

View workflow job for this annotation

GitHub Actions / core-js-101

Unexpected newline before ')'
}

Expand All @@ -503,7 +505,7 @@ function getIdentityMatrix(n) {
* 3, 3 => [ 3 ]
*/
function getIntervalArray(start, end) {
return Array.from({ length: end - start + 1 }, (_value, index) => start + index);
return Array.from({ length: end - start + 1 }, (_, index) => start + index);
}

/**
Expand Down Expand Up @@ -583,8 +585,7 @@ function group(array, keySelector, valueSelector) {
* ['one','two','three'], (x) => x.split('') => ['o','n','e','t','w','o','t','h','r','e','e']
*/
function selectMany(arr, childrenSelector) {
return arr.map((el) => childrenSelector(el))
.reduce((acc, curr) => (Array.isArray(curr) ? [...acc, ...curr] : acc), []);
return arr.map((el) => childrenSelector(el)).flatMap((el) => el);
}

/**
Expand Down Expand Up @@ -623,16 +624,18 @@ function getElementByIndexes(arr, indexes) {
*
*/
function swapHeadAndTail(arr) {
const arrayMiddle = arr.length / 2;
const arrayMiddleIndex = Math.floor(arrayMiddle);
const head = arr.slice(0, arrayMiddleIndex);
const tail = arr.slice(arrayMiddleIndex);

if (arrayMiddle === arrayMiddleIndex) {
return tail.concat(head);
if (arr.length <= 1) {
return arr;
}

return tail.slice(1).concat(arr[arrayMiddleIndex]).concat(head);
const middleIndex = Math.trunc(arr.length / 2);

const hasMiddleElement = arr.length % 2 !== 0;

const head = arr.slice(0, middleIndex);
const tail = arr.slice(middleIndex + Number(hasMiddleElement), arr.length);

return [...tail, ...(hasMiddleElement ? [arr[middleIndex]] : []), ...head];
}

module.exports = {
Expand Down

0 comments on commit 01abbed

Please sign in to comment.