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
76 changes: 44 additions & 32 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.split(',')[1].slice(0, -1).trim();
}


Expand All @@ -80,9 +80,7 @@ function extractNameFromTemplate(/* value */) {
* 'John Doe' => 'J'
* 'cat' => 'c'
*/
function getFirstChar(/* value */) {
throw new Error('Not implemented');
}
const getFirstChar = (value) => value.slice(0, 1);

/**
* Removes a leading and trailing whitespace characters from string.
Expand All @@ -95,8 +93,8 @@ function getFirstChar(/* value */) {
* 'cat' => 'cat'
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(/* value */) {
throw new Error('Not implemented');
function removeLeadingAndTrailingWhitespaces(value) {
return value.trim(0, -1);
}

/**
Expand All @@ -110,10 +108,11 @@ 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);
}


/**
* Remove the first occurrence of string inside another string
*
Expand All @@ -126,8 +125,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 +140,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 +155,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 +174,8 @@ function convertToUpperCase(/* str */) {
* ],
* '[email protected]' => ['[email protected]']
*/
function extractEmails(/* str */) {
throw new Error('Not implemented');
function extractEmails(str) {
return str.split(';');
}

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


/**
* Encode specified string with ROT13 cipher
* See details: https://en.wikipedia.org/wiki/ROT13
Expand All @@ -223,10 +224,15 @@ function getRectangleString(/* width, height */) {
* => 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
*
*/
function encodeToRot13(/* str */) {
throw new Error('Not implemented');
function encodeToRot13(str) {
const GAP = 13;
const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLM';
const lower = 'abcdefghijklmnopqrstuvwxyzabcdefghijklm';
const charEncoded = (char) => GAP + char.charCodeAt() - (char.charCodeAt() < 97 ? 65 : 97);
return str.replace(/[a-zA-Z]/g, (s) => (s.charCodeAt() < 97 ? upper[charEncoded(s)] : lower[charEncoded(s)]));
}


/**
* Returns true if the value is string; otherwise false.
* @param {string} value
Expand All @@ -240,8 +246,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 +275,14 @@ function isString(/* value */) {
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(/* value */) {
throw new Error('Not implemented');
function getCardId(value) {
const deck = [
'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 deck.indexOf(value);
}


Expand Down
58 changes: 34 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 radius * 2 * Math.PI;
}

/**
Expand All @@ -51,8 +51,11 @@ function getCircleCircumference(/* radius */) {
* 10, 0 => 5
* -3, 3 => 0
*/
function getAverage(/* value1, value2 */) {
throw new Error('Not implemented');
function getAverage(value1, value2) {
if (value1 === value2) return value1;
const vMax = Math.max(value1, value2);
const vMin = Math.min(value1, value2);
return vMin > 0 ? (vMax - vMin) / 2 : (vMax + vMin) / 2;
}

/**
Expand All @@ -70,8 +73,10 @@ 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) {
const a = x1 - x2;
const b = y1 - y2;
return Math.sqrt(a * a + b * b);
}

/**
Expand All @@ -86,8 +91,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 +114,9 @@ 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) {
return Math.acos((x1 * x2 + y1 * y2)
/ (Math.sqrt(x1 ** 2 + y1 ** 2) * Math.sqrt(x2 ** 2 + y2 ** 2)));
}

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


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

/**
Expand All @@ -158,8 +164,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 +186,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 +207,12 @@ function roundToPowerOfTen(/* num, pow */) {
* 16 => false
* 17 => true
*/
function isPrime(/* n */) {
throw new Error('Not implemented');
function isPrime(n) {
if (n <= 1) return false;
for (let i = 2; i <= Math.sqrt(n); i += 1) {
if (n % i === 0) return false;
}
return true;
}

/**
Expand All @@ -220,8 +230,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 (typeof +value === 'number' && !Number.isNaN(+value)) ? +value : def;
}

module.exports = {
Expand Down
Loading