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

Gorbunov 251051 #111

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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
41 changes: 26 additions & 15 deletions task/01-strings-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* '', 'bb' => 'bb'
*/
function concatenateStrings(value1, value2) {
throw new Error('Not implemented');
return value1 + value2;
}


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

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

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


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

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

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

/**
Expand All @@ -130,7 +130,7 @@ function repeatString(value, count) {
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(str, value) {
throw new Error('Not implemented');
return str.replace(value, '');
}

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


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

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

/**
Expand All @@ -201,7 +201,10 @@ function extractEmails(str) {
*
*/
function getRectangleString(width, height) {
throw new Error('Not implemented');
const topLine = '┌' + '─'.repeat(width - 2) + '┐\n';
const middleLine = '│' + ' '.repeat(width - 2) + '│\n';
const bottomLine = '└' + '─'.repeat(width - 2) + '┘\n';
return topLine + middleLine.repeat(height - 2) + bottomLine;
}


Expand All @@ -221,7 +224,10 @@ function getRectangleString(width, height) {
*
*/
function encodeToRot13(str) {
throw new Error('Not implemented');
const alpha = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const flipped = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm';
return str.replace(/[a-z]/gi, letter => flipped[alpha.indexOf(letter)]);
}
}

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


Expand Down Expand Up @@ -267,7 +273,12 @@ function isString(value) {
* 'K♠' => 51
*/
function getCardId(value) {
throw new Error('Not implemented');
const arr = ['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 arr.indexOf(value);
}


Expand Down
40 changes: 28 additions & 12 deletions task/02-numbers-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* 5, 5 => 25
*/
function getRectangleArea(width, height) {
throw new Error('Not implemented');
return width * height;
}


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

/**
Expand All @@ -54,7 +54,7 @@ function getCicleCircumference(radius) {
* -3, 3 => 0
*/
function getAverage(value1, value2) {
throw new Error('Not implemented');
return value1 / 2 + value2 / 2;
}

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

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


Expand All @@ -111,7 +111,17 @@ function getLinearEquationRoot(a, b) {
* (0,1) (1,2) => 0
*/
function getAngleBetweenVectors(x1, y1, x2, y2) {
throw new Error('Not implemented');
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 @@ -127,7 +137,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) {
* 0 => 0
*/
function getLastDigit(value) {
throw new Error('Not implemented');
return value % 10;
}


Expand All @@ -143,7 +153,7 @@ function getLastDigit(value) {
* '-525.5' => -525.5
*/
function parseNumberFromString(value) {
throw new Error('Not implemented');
return +value;
}

/**
Expand All @@ -160,7 +170,7 @@ function parseNumberFromString(value) {
* 1,2,3 => 3.741657386773941
*/
function getParallelipidedDiagonal(a,b,c) {
throw new Error('Not implemented');
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2) + Math.pow(c, 2));
}

/**
Expand All @@ -181,7 +191,7 @@ function getParallelipidedDiagonal(a,b,c) {
* 1678, 3 => 2000
*/
function roundToPowerOfTen(num, pow) {
throw new Error('Not implemented');
return Math.round(num / Math.pow(10, pow)) * Math.pow(10, pow);
}

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

return true;
}

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

module.exports = {
Expand Down
34 changes: 29 additions & 5 deletions task/03-date-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* 'Sun, 17 May 1998 03:00:00 GMT+01' => Date()
*/
function parseDataFromRfc2822(value) {
throw new Error('Not implemented');
return new Date(value);
}

/**
Expand All @@ -37,7 +37,7 @@ function parseDataFromRfc2822(value) {
* '2016-01-19T08:07:37Z' => Date()
*/
function parseDataFromIso8601(value) {
throw new Error('Not implemented');
return new Date(value);
}


Expand All @@ -56,7 +56,18 @@ function parseDataFromIso8601(value) {
* Date(2015,1,1) => false
*/
function isLeapYear(date) {
throw new Error('Not implemented');
const year = date.getFullYear();

if (year % 4 != 0) {
return false;
}
else if (year % 100 != 0) {
return true;
}
else if (year % 400 != 0) {
return false;
}
else return true;
}


Expand All @@ -76,7 +87,12 @@ function isLeapYear(date) {
* Date(2000,1,1,10,0,0), Date(2000,1,1,15,20,10,453) => "05:20:10.453"
*/
function timeSpanToString(startDate, endDate) {
throw new Error('Not implemented');
const rez = endDate - startDate;
let h = Math.trunc(rez / 3600000 % 100).toString().padStart(2, '0');
let m = Math.trunc(rez / 60000 % 60).toString().padStart(2, '0');
let s = Math.trunc(rez / 1000 % 60).toString().padStart(2, '0');
let ms = Math.trunc(rez % 1000).toString().padStart(3, '0');
return (h + ':' + m + ':' + s + '.' + ms);
}


Expand All @@ -94,7 +110,15 @@ function timeSpanToString(startDate, endDate) {
* Date.UTC(2016,3,5,21, 0) => Math.PI/2
*/
function angleBetweenClockHands(date) {
throw new Error('Not implemented');
let hours = date.getUTCHours();
let minutes = date.getUTCMinutes();
hours = (hours > 12 ? hours - 12 : hours);
let angle = Math.abs(0.5 * (60 * hours - 11 * minutes))

if (angle > 180) {
angle -= 180;
}
return angle * Math.PI / 180;
}


Expand Down
Loading