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

Готовое задание #89

Open
wants to merge 1 commit 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
84 changes: 63 additions & 21 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.concat(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 new RegExp("Hello, (.+)!").exec(value)[1]
}


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

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

/**
* Remove the first occurrence of string inside another string
*
*
* @param {string} str
* @param {string} value
* @return {string}
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.replace('<', '').replace('>', '')
}


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,25 @@ function extractEmails(str) {
*
*/
function getRectangleString(width, height) {
throw new Error('Not implemented');
var horizontalChar = '─';
var verticalChar = '│';
var innerChar = ' ';
var breakChar = '\n';
var str = '';

for(var h = 1; h <= height; h++) {
switch (h) {
case 1:
str += '┌' + horizontalChar.repeat(width-2) + '┐' + breakChar
break
case height:
str += '└' + horizontalChar.repeat(width-2) + '┘' + breakChar
break
default:
str += verticalChar + innerChar.repeat(width-2) + verticalChar + breakChar
}
}
return str
}


Expand All @@ -221,7 +239,25 @@ function getRectangleString(width, height) {
*
*/
function encodeToRot13(str) {
throw new Error('Not implemented');
var amount = 13
var output = ""

for (var i = 0; i < str.length; i++) {
var c = str[i]
if (c.match(/[a-z]/i)) {
var code = str.charCodeAt(i)

if (code >= 65 && code <= 90) {
c = String.fromCharCode(((code - 65 + amount) % 26) + 65)
}
else if (code >= 97 && code <= 122) {
c = String.fromCharCode(((code - 97 + amount) % 26) + 97)
}
}
output += c
}

return output
}

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


/**
* Returns playid card id.
*
*
* Playing cards inittial deck inclides the cards in the following order:
*
*
* '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♠'
*
*
* (see https://en.wikipedia.org/wiki/Standard_52-card_deck)
* Function returns the zero-based index of specified card in the initial deck above.
*
*
* @param {string} value
* @return {number}
*
* @example
* 'A♣' => 0
* '2♣' => 1
* '2♣' => 1
* '3♣' => 2
* ...
* 'Q♠' => 50
* 'K♠' => 51
*/
function getCardId(value) {
throw new Error('Not implemented');
const colors = '♣♦♥♠',
nums = 'A234567891JQK';

let color = value[value.length - 1],
num = value[0];

return colors.indexOf(color) * 13 + nums.indexOf(num);
}


Expand Down
43 changes: 28 additions & 15 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 Math.PI * radius * 2;
}

/**
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,9 @@ function getAverage(value1, value2) {
* (-5,0) (10,-10) => 18.027756377319946
*/
function getDistanceBetweenPoints(x1, y1, x2, y2) {
throw new Error('Not implemented');
var a = x1 - x2;
var b = y1 - y2;
return Math.sqrt(a*a + b*b)
}

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


Expand All @@ -111,7 +113,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 +133,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) {
* 0 => 0
*/
function getLastDigit(value) {
throw new Error('Not implemented');
return Math.abs(value) % 10
}


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

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

/**
Expand All @@ -169,7 +175,7 @@ function getParallelipidedDiagonal(a,b,c) {
* @param {number} num
* @param {number} pow
* @return {number}
*
*
* @example:
* 1234, 0 => 1234
* 1234, 1 => 1230
Expand All @@ -181,7 +187,10 @@ function getParallelipidedDiagonal(a,b,c) {
* 1678, 3 => 2000
*/
function roundToPowerOfTen(num, pow) {
throw new Error('Not implemented');
if (num % Math.pow(10, pow) >= 5 * Math.pow(10, pow - 1)) {
return num + ( Math.pow(10, pow) - num % Math.pow(10, pow));
}
return num - (num % Math.pow(10, pow));
}

/**
Expand All @@ -190,7 +199,7 @@ function roundToPowerOfTen(num, pow) {
*
* @param {number} n
* @return {bool}
*
*
* @example:
* 4 => false
* 5 => true
Expand All @@ -202,7 +211,11 @@ 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 +234,7 @@ function isPrime(n) {
* toNumber(new Number(42), 0) => 42
*/
function toNumber(value, def) {
throw new Error('Not implemented');
return typeof +value == 'number' && !isNaN(+value) ? +value : def;
}

module.exports = {
Expand All @@ -237,4 +250,4 @@ module.exports = {
roundToPowerOfTen: roundToPowerOfTen,
isPrime: isPrime,
toNumber: toNumber
};
};
Loading