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
Binary file added .DS_Store
Binary file not shown.
3,100 changes: 3,098 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

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


/**
* Returns the length of given string.
*
Expand All @@ -34,10 +33,11 @@ function concatenateStrings(/* value1, value2 */) {
* 'b' => 1
* '' => 0
*/
function getStringLength(/* value */) {
throw new Error('Not implemented');
function getStringLength(value) {
return value.length;
}


/**
* Returns the result of string template and given parameters firstName and lastName.
* Please do not use concatenation, use template string :
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.charAt(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);
}

/**
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.replace(/[{< >}]/g, '');
}


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.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
}

/**
Expand All @@ -202,8 +202,11 @@ 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;
}


Expand All @@ -223,8 +226,10 @@ function getRectangleString(/* width, height */) {
* => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
*
*/
function encodeToRot13(/* str */) {
throw new Error('Not implemented');
function encodeToRot13(str) {
const originalAlpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
const cipher = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
return str.replace(/[a-z]/gi, (letter) => cipher[originalAlpha.indexOf(letter)]);
}

/**
Expand All @@ -240,11 +245,10 @@ 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;
}


/**
* Returns playid card id.
*
Expand All @@ -269,11 +273,14 @@ function isString(/* value */) {
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(/* value */) {
throw new Error('Not implemented');
function getCardId(value) {
const arrCards = ['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 arrCards.indexOf(value);
}


module.exports = {
concatenateStrings,
getStringLength,
Expand Down
61 changes: 37 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 Number(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 ** 2 + b ** 2 + c ** 2);
}


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

/**
Expand All @@ -201,8 +209,13 @@ function roundToPowerOfTen(/* num, pow */) {
* 16 => false
* 17 => true
*/
function isPrime(/* n */) {
throw new Error('Not implemented');
function isPrime(n) {
for (let i = 2; n > i; i += 1) {
if (n % i === 0) {
return false;
}
}
return n > 1;
}

/**
Expand All @@ -220,8 +233,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