Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor core-js-101 tasks solutions #6

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions src/01-strings-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* '', 'bb' => 'bb'
*/
function concatenateStrings(value1, value2) {
return value1 + value2;
return value1.concat(value2);
}

/**
Expand Down Expand Up @@ -78,7 +78,11 @@ function extractNameFromTemplate(value) {
* 'cat' => 'c'
*/
function getFirstChar(value) {
return value[0];
if (value === '') {
return value;
}

return value.charAt(0);
}

/**
Expand Down Expand Up @@ -108,6 +112,10 @@ function removeLeadingAndTrailingWhitespaces(value) {
* 'cat', 3 => 'catcatcat'
*/
function repeatString(value, count) {
if (count < 0) {
return '';
}

return value.repeat(count);
}

Expand Down Expand Up @@ -240,20 +248,14 @@ function encodeToRot13(str) {
const input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const output = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';

let result = '';
let index = -1;

for (let i = 0; i <= str.length - 1; i += 1) {
index = input.indexOf(str[i]);
return str
.split('')
.map((char) => {
const inputIndex = input.indexOf(char);

if (index !== -1) {
result += output[index];
} else {
result += str[i];
}
}

return result;
return inputIndex !== -1 ? output[inputIndex] : char;
})
.join('');
}

/**
Expand All @@ -274,9 +276,9 @@ function isString(value) {
}

/**
* Returns playid card id.
* Return card id.
*
* Playing cards inittial deck inclides the cards in the following order:
* Playing cards initial deck includes the cards in the following order:
*
* 'A♣','2♣','3♣','4♣','5♣','6♣','7♣','8♣','9♣','10♣','J♣','Q♣','K♣',
* 'A♦','2♦','3♦','4♦','5♦','6♦','7♦','8♦','9♦','10♦','J♦','Q♦','K♦',
Expand Down
6 changes: 3 additions & 3 deletions src/02-numbers-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function getRectangleArea(width, height) {
* 0 => 0
*/
function getCircleCircumference(radius) {
return 2 * radius * Math.PI;
return 2 * Math.PI * radius;
}

/**
Expand Down Expand Up @@ -141,7 +141,7 @@ function getLastDigit(value) {
* '-525.5' => -525.5
*/
function parseNumberFromString(value) {
return +value;
return Number(value);
}

/**
Expand Down Expand Up @@ -179,7 +179,7 @@ function getParallelepipedDiagonal(a, b, c) {
* 1678, 3 => 2000
*/
function roundToPowerOfTen(num, pow) {
return pow === 0 ? num : Math.round(num / (10 ** pow)) * (10 ** pow);
return pow === 0 ? num : Math.round(num / 10 ** pow) * 10 ** pow;
}

/**
Expand Down
79 changes: 41 additions & 38 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),
],
[],
);
}

/**
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],
);
}

/**
* 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,17 +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) => {
if (cellIndex === columnIndex) {
return 1;
}

return 0;
}));
}

/**
Expand All @@ -503,7 +504,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 @@ -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
Loading