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

lab-3 251051 Birukov #109

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ To start javascript assignments please follow the next steps:
git commit -m "Update the links"
git push origin master
```
* Open https://github.com/rolling-scopes-school/js-assignments and test the build icon. Now it will run all tests and update status once you push changes to github. Keep this icon green!
* Open https://github.com/birukov01/js-assignments and test the build icon. Now it will run all tests and update status once you push changes to github. Keep this icon green!


### How to setup work environment
Expand Down
115 changes: 100 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,10 @@ function getStringFromTemplate(firstName, lastName) {
* 'Hello, Chuck Norris!' => 'Chuck Norris'
*/
function extractNameFromTemplate(value) {
throw new Error('Not implemented');
let str = '';
let i = value.indexOf(' ') + 1;
for (; value[i] != '!'; i++) str += value[i];
return str;
}


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

/**
Expand All @@ -99,7 +102,17 @@ function getFirstChar(value) {
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(value) {
throw new Error('Not implemented');
let str = '';
let l = 0;
let t = value.length - 1;
while (value[l] == ' ' || value[l] == '\t') l++;
while (value[t] == ' ' || value[t] == '\t') t--;
while (l <= t)
{
str += value[l];
l++;
}
return str;
}

/**
Expand All @@ -114,7 +127,9 @@ function removeLeadingAndTrailingWhitespaces(value) {
* 'cat', 3 => 'catcatcat'
*/
function repeatString(value, count) {
throw new Error('Not implemented');
let str = '';
for (let i = 0; i < count; i++) str += value;
return str;
}

/**
Expand All @@ -130,7 +145,18 @@ function repeatString(value, count) {
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(str, value) {
throw new Error('Not implemented');
let strN = '';
let posV = str.indexOf(value);
for (let i = 0; i < str.length; i++)
{
if (i == posV)
{
i += value.length;
if (str[posV - 1] == ' ' && str[i] == ' ') i++;
}
strN += str[i];
}
return strN;
}

/**
Expand All @@ -145,7 +171,11 @@ function removeFirstOccurrences(str, value) {
* '<a>' => 'a'
*/
function unbracketTag(str) {
throw new Error('Not implemented');
let strN = '';
for (let i = 0; i < str.length; i++)
if (str[i] != '<' && str[i] != '>')
strN += str[i];
return strN;
}


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

/**
Expand All @@ -174,7 +204,21 @@ function convertToUpperCase(str) {
* '[email protected]' => ['[email protected]']
*/
function extractEmails(str) {
throw new Error('Not implemented');
let arr = [];
let iS = 0;
let iA = 0;
while (iS < str.length)
{
if (str[iS] == ';') iS++;
arr[iA] = '';
while (str[iS] != ';' && iS < str.length)
{
arr[iA] += str[iS];
iS++;
}
iA++;
}
return arr;
}

/**
Expand All @@ -201,7 +245,20 @@ function extractEmails(str) {
*
*/
function getRectangleString(width, height) {
throw new Error('Not implemented');
let str = '';
str += ' ┌';
for (let i = 0; i < width - 2; i++) str += '─';
str += '┐\n';
for (let i = 0; i < height - 2; i++)
{
str += ' │';
for (let i = 0; i < width - 2; i++) str += ' ';
str += '│\n';
}
str += ' └';
for (let i = 0; i < width - 2; i++) str += '─';
str += '┘\n';
return str;
}


Expand All @@ -221,7 +278,21 @@ function getRectangleString(width, height) {
*
*/
function encodeToRot13(str) {
throw new Error('Not implemented');
let strN = '';
let a = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz';
let p;
for (let i = 0; i < str.length; i++)
{
p = a.indexOf(str[i]);
if (p == -1) strN += str[i];
else
{
p += 26;
if (p > 51) p -= 52;
strN += a[p];
}
}
return strN;
}

/**
Expand All @@ -238,7 +309,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 +338,21 @@ function isString(value) {
* 'K♠' => 51
*/
function getCardId(value) {
throw new Error('Not implemented');
let str1 = 'A23456789dJQK';
let str2 = '♣♦♥♠';
let p1;
let p2;
if (value[1] == '0')
{
p1 = str1.indexOf('d');
p2 = str2.indexOf(value[2]);
}
else
{
p1 = str1.indexOf(value[0]);
p2 = str2.indexOf(value[1]);
}
return p2 * 13 + p1;
}


Expand Down
32 changes: 20 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 + 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.pow(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2), 0.5);
}

/**
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,9 @@ 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 +129,7 @@ function getAngleBetweenVectors(x1, y1, x2, y2) {
* 0 => 0
*/
function getLastDigit(value) {
throw new Error('Not implemented');
return value%10;
}


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

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

/**
Expand All @@ -181,7 +183,9 @@ 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 = Math.round(num / a);
return b*a;
}

/**
Expand All @@ -202,7 +206,9 @@ 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 +227,9 @@ 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
15 changes: 10 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 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,11 @@ 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