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

251051 Марчук #107

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
46 changes: 29 additions & 17 deletions task/01-strings-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* *
* Plese read the following tutorial before implementing tasks: *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String *
* *
* npm test ./test/01-strings-tests.js *
********************************************************************************************/


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


/**
* Returns a first char of the given string.
*
Expand All @@ -84,7 +83,7 @@ function extractNameFromTemplate(value) {
* 'cat' => 'c'
*/
function getFirstChar(value) {
throw new Error('Not implemented');
return value[0];
}

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

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

/**
Expand All @@ -130,7 +129,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 +144,7 @@ function removeFirstOccurrences(str, value) {
* '<a>' => 'a'
*/
function unbracketTag(str) {
throw new Error('Not implemented');
return str.replace(/[<,>]/gi,'');
}


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

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

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


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

/**
Expand All @@ -238,7 +243,7 @@ function encodeToRot13(str) {
* isString(new String('test')) => true
*/
function isString(value) {
throw new Error('Not implemented');
return Object.prototype.toString.call(value) === "[object String]"
}


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


Expand Down
51 changes: 38 additions & 13 deletions task/02-numbers-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math *
* *
* npm test ./test/02-numbers-tests.js *
********************************************************************************************/


Expand All @@ -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,11 @@ function getCicleCircumference(radius) {
* -3, 3 => 0
*/
function getAverage(value1, value2) {
throw new Error('Not implemented');
/*let a=Number(value1);
let b=Number(value2);
let res = (a+b)/2;
return res;*/
return value1/2 + value2/2;
}

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

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


Expand All @@ -111,7 +117,11 @@ function getLinearEquationRoot(a, b) {
* (0,1) (1,2) => 0
*/
function getAngleBetweenVectors(x1, y1, x2, y2) {
throw new Error('Not implemented');
var angle = Math.atan2(y2, x2) - Math.atan2(y1, x1)
if (angle < 0) {
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 Number(value);
}

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

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

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

/**
Expand All @@ -221,7 +241,12 @@ function isPrime(n) {
* toNumber(new Number(42), 0) => 42
*/
function toNumber(value, def) {
throw new Error('Not implemented');
const num = Number(value);
if(isNaN(num)){
return def;
}else{
return num;
}
}

module.exports = {
Expand Down
19 changes: 13 additions & 6 deletions task/03-date-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Plese read the following tutorial before implementing tasks: *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Numbers_and_dates#Date_object
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date *
* *
* npm test ./test/03-date-tests.js *
********************************************************************************************/


Expand All @@ -22,7 +22,7 @@
* 'Sun, 17 May 1998 03:00:00 GMT+01' => Date()
*/
function parseDataFromRfc2822(value) {
throw new Error('Not implemented');
return Date.parse(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 Date.parse(value);
}


Expand All @@ -56,7 +56,8 @@ function parseDataFromIso8601(value) {
* Date(2015,1,1) => false
*/
function isLeapYear(date) {
throw new Error('Not implemented');
let year = date.getFullYear();
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}


Expand All @@ -76,7 +77,7 @@ 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');
return (new Date(endDate - startDate)).toISOString().slice(11, -1);
}


Expand All @@ -94,7 +95,13 @@ function timeSpanToString(startDate, endDate) {
* Date.UTC(2016,3,5,21, 0) => Math.PI/2
*/
function angleBetweenClockHands(date) {
throw new Error('Not implemented');
const hoursArrow = 0.5 * (60 * (date.getUTCHours() % 12) + date.getUTCMinutes());
const minutesArrow = 6 * date.getUTCMinutes();
let angle = Math.abs(hoursArrow - minutesArrow);
if (angle > 180) {
angle = 360 - angle;
}
return (angle * Math.PI / 180);
}


Expand Down
Loading