Skip to content
Open
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
3,100 changes: 3,098 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

88 changes: 57 additions & 31 deletions src/01-strings-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* 'aa','' => 'aa'
* '', 'bb' => 'bb'
*/
function concatenateStrings(/* value1, value2 */) {
throw new Error('Not implemented');
function concatenateStrings(value1, value2) {
return value1 + value2;
}


Expand All @@ -34,8 +34,8 @@ function concatenateStrings(/* value1, value2 */) {
* 'b' => 1
* '' => 0
*/
function getStringLength(/* value */) {
throw new Error('Not implemented');
function getStringLength(value) {
return value.length;
}

/**
Expand All @@ -51,8 +51,8 @@ function getStringLength(/* value */) {
* 'John','Doe' => 'Hello, John Doe!'
* 'Chuck','Norris' => 'Hello, Chuck Norris!'
*/
function getStringFromTemplate(/* firstName, lastName */) {
throw new Error('Not implemented');
function getStringFromTemplate(firstName, lastName) {
return `Hello, ${firstName} ${lastName}!`;
}

/**
Expand All @@ -65,8 +65,8 @@ function getStringFromTemplate(/* firstName, lastName */) {
* 'Hello, John Doe!' => 'John Doe'
* 'Hello, Chuck Norris!' => 'Chuck Norris'
*/
function extractNameFromTemplate(/* value */) {
throw new Error('Not implemented');
function extractNameFromTemplate(value) {
return value.slice(7, -1);
}


Expand All @@ -80,8 +80,8 @@ function extractNameFromTemplate(/* value */) {
* 'John Doe' => 'J'
* 'cat' => 'c'
*/
function getFirstChar(/* value */) {
throw new Error('Not implemented');
function getFirstChar(value) {
return value[0];
}

/**
Expand All @@ -95,8 +95,8 @@ function getFirstChar(/* value */) {
* 'cat' => 'cat'
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(/* value */) {
throw new Error('Not implemented');
function removeLeadingAndTrailingWhitespaces(value) {
return value.trim();
}

/**
Expand All @@ -110,8 +110,8 @@ function removeLeadingAndTrailingWhitespaces(/* value */) {
* 'A', 5 => 'AAAAA'
* 'cat', 3 => 'catcatcat'
*/
function repeatString(/* value, count */) {
throw new Error('Not implemented');
function repeatString(value, count) {
return value.repeat(count).toString();
}

/**
Expand All @@ -126,8 +126,8 @@ function repeatString(/* value, count */) {
* 'I like legends', 'end' => 'I like legs',
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(/* str, value */) {
throw new Error('Not implemented');
function removeFirstOccurrences(str, value) {
return str.replace(value, '');
}

/**
Expand All @@ -141,8 +141,8 @@ function removeFirstOccurrences(/* str, value */) {
* '<span>' => 'span'
* '<a>' => 'a'
*/
function unbracketTag(/* str */) {
throw new Error('Not implemented');
function unbracketTag(str) {
return str.slice(1, -1);
}


Expand All @@ -156,8 +156,8 @@ function unbracketTag(/* str */) {
* 'Thunderstruck' => 'THUNDERSTRUCK'
* 'abcdefghijklmnopqrstuvwxyz' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
*/
function convertToUpperCase(/* str */) {
throw new Error('Not implemented');
function convertToUpperCase(str) {
return str.toUpperCase();
}

/**
Expand All @@ -175,8 +175,8 @@ function convertToUpperCase(/* str */) {
* ],
* '[email protected]' => ['[email protected]']
*/
function extractEmails(/* str */) {
throw new Error('Not implemented');
function extractEmails(str) {
return str.split(/\s*;\s*/);
}

/**
Expand All @@ -202,11 +202,13 @@ function extractEmails(/* str */) {
* '└──────────┘\n'
*
*/
function getRectangleString(/* width, height */) {
throw new Error('Not implemented');
function getRectangleString(width, height) {
const head = `┌${'─'.repeat(width - 2)}┐\n`;
const midle = `│${' '.repeat(width - 2)}│\n`;
const foot = `└${'─'.repeat(width - 2)}┘\n`;
return head + midle.repeat(height - 2) + foot;
}


/**
* Encode specified string with ROT13 cipher
* See details: https://en.wikipedia.org/wiki/ROT13
Expand All @@ -223,8 +225,27 @@ function getRectangleString(/* width, height */) {
* => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
*
*/
function encodeToRot13(/* str */) {
throw new Error('Not implemented');
function encodeToRot13(str) {
const abcNorm = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const abcRor13 = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
// let strEncode = '';
// for (const i of str) {
// const index = abcNorm.indexOf(i);
// strEncode += index === -1 ? i : abcRor13[index];
// }
// return strEncode;

let strEncode = '';
for (let i = 0; i < str.length; i += 1) {
const index = abcNorm.indexOf(str[i]);
if (index === -1) {
strEncode += str[i];
} else {
strEncode += abcRor13[index];
}
}
return strEncode;
// throw new Error('Not implemented');
}

/**
Expand All @@ -240,8 +261,8 @@ function encodeToRot13(/* str */) {
* isString('test') => true
* isString(new String('test')) => true
*/
function isString(/* value */) {
throw new Error('Not implemented');
function isString(value) {
return typeof value === 'string' || value instanceof String;
}


Expand Down Expand Up @@ -269,8 +290,13 @@ function isString(/* value */) {
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(/* value */) {
throw new Error('Not implemented');
function getCardId(value) {
const cardsArray = [
'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♦',
'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♠'];
return cardsArray.indexOf(value);
}


Expand Down
71 changes: 47 additions & 24 deletions src/02-numbers-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
* 5, 10 => 50
* 5, 5 => 25
*/
function getRectangleArea(/* width, height */) {
throw new Error('Not implemented');
function getRectangleArea(width, height) {
return width * height;
}


Expand All @@ -35,8 +35,8 @@ function getRectangleArea(/* width, height */) {
* 3.14 => 19.729201864543903
* 0 => 0
*/
function getCircleCircumference(/* radius */) {
throw new Error('Not implemented');
function getCircleCircumference(radius) {
return 2 * Math.PI * radius;
}

/**
Expand All @@ -51,8 +51,8 @@ function getCircleCircumference(/* radius */) {
* 10, 0 => 5
* -3, 3 => 0
*/
function getAverage(/* value1, value2 */) {
throw new Error('Not implemented');
function getAverage(value1, value2) {
return value1 / 2 + value2 / 2;
}

/**
Expand All @@ -70,8 +70,8 @@ function getAverage(/* value1, value2 */) {
* (0,0) (1,0) => 1
* (-5,0) (10,-10) => 18.027756377319946
*/
function getDistanceBetweenPoints(/* x1, y1, x2, y2 */) {
throw new Error('Not implemented');
function getDistanceBetweenPoints(x1, y1, x2, y2) {
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
}

/**
Expand All @@ -86,8 +86,8 @@ function getDistanceBetweenPoints(/* x1, y1, x2, y2 */) {
* x + 8 = 0 => -8
* 5*x = 0 => 0
*/
function getLinearEquationRoot(/* a, b */) {
throw new Error('Not implemented');
function getLinearEquationRoot(a, b) {
return -b / a;
}


Expand All @@ -109,8 +109,16 @@ function getLinearEquationRoot(/* a, b */) {
* (0,1) (0,1) => 0
* (0,1) (1,2) => 0
*/
function getAngleBetweenVectors(/* x1, y1, x2, y2 */) {
throw new Error('Not implemented');
function getAngleBetweenVectors(x1, y1, x2, y2) {
let angle = Math.atan2(x1, y1) - Math.atan2(x2, y2);
if (angle < 0) {
angle += 2 * Math.PI;
}
if (angle > Math.PI) {
angle -= 2 * Math.PI;
}

return angle;
}

/**
Expand All @@ -125,8 +133,8 @@ function getAngleBetweenVectors(/* x1, y1, x2, y2 */) {
* 5 => 5
* 0 => 0
*/
function getLastDigit(/* value */) {
throw new Error('Not implemented');
function getLastDigit(value) {
return value % 10;
}


Expand All @@ -141,8 +149,8 @@ function getLastDigit(/* value */) {
* '37' => 37
* '-525.5' => -525.5
*/
function parseNumberFromString(/* value */) {
throw new Error('Not implemented');
function parseNumberFromString(value) {
return parseFloat(value);
}

/**
Expand All @@ -158,8 +166,8 @@ function parseNumberFromString(/* value */) {
* 3,3,3 => 5.196152422706632
* 1,2,3 => 3.741657386773941
*/
function getParallelepipedDiagonal(/* a, b, c */) {
throw new Error('Not implemented');
function getParallelepipedDiagonal(a, b, c) {
return Math.sqrt(a * a + b * b + c * c);
}


Expand All @@ -180,8 +188,11 @@ function getParallelepipedDiagonal(/* a, b, c */) {
* 1678, 2 => 1700
* 1678, 3 => 2000
*/
function roundToPowerOfTen(/* num, pow */) {
throw new Error('Not implemented');
function roundToPowerOfTen(num, pow) {
if (pow === 0) {
return num;
}
return Math.round(num / (10 ** pow)) * (10 ** pow);
}

/**
Expand All @@ -201,8 +212,20 @@ function roundToPowerOfTen(/* num, pow */) {
* 16 => false
* 17 => true
*/
function isPrime(/* n */) {
throw new Error('Not implemented');
function isPrime(n) {
if (n === 2 || n === 3) {
return true;
}
if (n <= 1 || n % 2 === 0 || n % 3 === 0) {
return false;
}

for (let i = 5; i * i <= n; i += 6) {
if (n % i === 0 || n % (i + 2) === 0) {
return false;
}
}
return true;
}

/**
Expand All @@ -220,8 +243,8 @@ function isPrime(/* n */) {
* toNumber(42, 0) => 42
* toNumber(new Number(42), 0) => 42
*/
function toNumber(/* value, def */) {
throw new Error('Not implemented');
function toNumber(value, def) {
return Number(value) || def;
}

module.exports = {
Expand Down
Loading