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

#1-4 Tasks. Alexey_Karavaychik #824

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
language: node_js
node_js:
- "5.10.0"
- "12.18.2"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[![Brest Rolling Scopes](http://brest.rollingscopes.com/images/logo_rs_text.svg)](http://brest.rollingscopes.com/)
#Brest Rolling Scopes School
## Javascript Assignments [![Build Status](https://travis-ci.org/AisBrestEDU/js-assignments.svg?branch=master)](https://travis-ci.org/AisBrestEDU/js-assignments)
## Javascript Assignments [![Build Status](https://travis-ci.org/AlexGit2012/js-assignments.svg?branch=master)](https://travis-ci.org/AlexGit2012/js-assignments)

Yet another javascript assignments. There are a lot of interactive javascript resources for beginners, but most of them are online and do not cover the modern programming workflow. There are some excellent training resources on github (https://github.com/rmurphey/js-assessment, https://github.com/mrdavidlaing/javascript-koans, https://github.com/vasanthk/js-bits etc) but they are not exactly simulate the everyday programming process. So the motivation of this project is to show TDD process in the wild to the beginners. Assingment tests are implemented in various ways to feel a difference and gain the experience what manner is good, what is bad and what is ugly.

@@ -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/AisBrestEDU/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/AlexGit2012/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
@@ -73,7 +73,7 @@ and run the unit tests again. Find one test failed (red). Now it's time to fix i
* Implement the function by any way and verify your solution by running tests until the failed test become passed (green).
* Your solution work, but now time to refactor it. Try to make your code as pretty and simple as possible keeping up the test green.
* Once you can't improve your code and tests are passed you can commit your solution.
* Push your updates to github server and check if tests passed on [travis-ci](https://travis-ci.org/AisBrestEDU/js-assignments/builds).
* Push your updates to github server and check if tests passed on [travis-ci](https://travis-ci.org/AlexGit2012/js-assignments/builds).
* If everything is OK you can try to resolve the next task.

### How to debug tasks
68 changes: 52 additions & 16 deletions task/01-strings-tasks.js
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

/********************************************************************************************
* *
* Plese read the following tutorial before implementing tasks: *
* Please read the following tutorial before implementing tasks: *
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String *
* *
********************************************************************************************/
@@ -22,7 +22,7 @@
* '', 'bb' => 'bb'
*/
function concatenateStrings(value1, value2) {
throw new Error('Not implemented');
return (value1+value2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces around operators

Please, try to follow these rules https://javascript.info/coding-style

}


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

/**
@@ -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+"!")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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


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

/**
@@ -99,7 +99,13 @@ function getFirstChar(value) {
* '\tHello, World! ' => 'Hello, World!'
*/
function removeLeadingAndTrailingWhitespaces(value) {
throw new Error('Not implemented');
while (value[0] === (" ") || value[0] === ("\t")) {
value = (value[0] === (" ") || value[0] === ("\t") ? value.substring(1) : value)
}
while (value[value.length-1] === (" ") || value[value.length-1] === ("\t")) {
value = (value[value.length-1] === (" ") || value[value.length-1] === ("\t") ? value.substring(0,value.length-1) : value)
}
return value
Comment on lines +102 to +108
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

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

/**
@@ -130,7 +136,8 @@ function repeatString(value, count) {
* 'ABABAB','BA' => 'ABAB'
*/
function removeFirstOccurrences(str, value) {
throw new Error('Not implemented');
let pos = str.indexOf(value)
return str.substring(0,pos)+str.substring(pos+value.length,str.length)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

/**
@@ -145,7 +152,7 @@ function removeFirstOccurrences(str, value) {
* '<a>' => 'a'
*/
function unbracketTag(str) {
throw new Error('Not implemented');
return str.substring(1,str.length-1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

space between parameters

}


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

/**
@@ -174,7 +181,9 @@ function convertToUpperCase(str) {
* '[email protected]' => ['[email protected]']
*/
function extractEmails(str) {
throw new Error('Not implemented');
let newArrayOfStr = str.split(";")
newArrayOfStr.forEach((el) => ("'"+el+"'"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is unnecessary. It hasn't any impact on the result, because it doesn't mutate initial array and returns undefined

https://stackoverflow.com/questions/34426458/javascript-difference-between-foreach-and-map

return newArrayOfStr
}

/**
@@ -201,7 +210,20 @@ function extractEmails(str) {
*
*/
function getRectangleString(width, height) {
throw new Error('Not implemented');
let str = ""
for (let h=1;h<=height;h++) {
for (let w=1; w<=width; w++) {
((h===1)&&(w===1)) && (str+="┌");
(((h===1)||(h===height)) && (w!==1 && w!==width) && (str+="─"));
((h===1)&&(w===width)) && (str+="┐\n");
(w===1) && (h!==1 && h!==height) && (str+="│");
(w===width) && (h!==1 && h!==height) && (str+="│\n");
(h>1 && h<height)&&(w>1 && w<width) && (str+=" ");
(w===1) && (h===height) && (str+="└");
(w===width)&&(h===height) && (str+="┘\n")
Comment on lines +213 to +223
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spaces around operators

}
}
return str
}


@@ -221,7 +243,18 @@ function getRectangleString(width, height) {
*
*/
function encodeToRot13(str) {
throw new Error('Not implemented');
let newStr = str.split("");
let alphabetLowerCase = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
let alphabetUpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (let i=0; i<str.length; i++) {
if (alphabetUpperCase.includes(newStr[i])) {
newStr[i]=alphabetUpperCase[alphabetUpperCase.indexOf(str[i])+13]
}
if (alphabetLowerCase.includes(newStr[i])) {
newStr[i]=alphabetLowerCase[alphabetLowerCase.indexOf(str[i])+13]
}
}
return newStr.toString().split(",").join("")
}

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


@@ -267,7 +301,9 @@ function isString(value) {
* 'K♠' => 51
*/
function getCardId(value) {
throw new Error('Not implemented');
let cardSuit = "♣♦♥♠";
let cardValue = "A234567891JQK";
return cardSuit.indexOf(value[value.length-1])*13+cardValue.indexOf(value[0]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}


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


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

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

/**
@@ -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((x2-x1)**2+(y2-y1)**2)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, improve using Math.hypot

}

/**
@@ -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===0 ? b/a : -b/a
}


@@ -111,7 +111,7 @@ function getLinearEquationRoot(a, b) {
* (0,1) (1,2) => 0
*/
function getAngleBetweenVectors(x1, y1, x2, y2) {
throw new Error('Not implemented');
return Math.acos((x1*x2+y1*y2)/(Math.sqrt(x1**2+y1**2)*(Math.sqrt(x2**2+y2**2))))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, improve using Math.hypot

}

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


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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, improve using Math.hypot

}

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

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

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

module.exports = {
20 changes: 15 additions & 5 deletions task/03-date-tasks.js
Original file line number Diff line number Diff line change
@@ -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)
}

/**
@@ -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)
}


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


@@ -76,7 +77,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');
let date = new Date(endDate.getTime() - startDate.getTime());
let days = endDate.getDay()- startDate.getDay();
return ""+((date.getUTCHours()+24*days)<10 ? "0"+date.getUTCHours() : date.getUTCHours()+24*days)+":"
+(date.getUTCMinutes()<10 ? "0"+date.getUTCMinutes() : date.getUTCMinutes())+":"
+(date.getUTCSeconds()<10 ? "0"+date.getUTCSeconds() : date.getUTCSeconds())+"."
+(date.getUTCMilliseconds()<100 ? (date.getUTCMilliseconds()<10 ? "00"+date.getUTCMilliseconds() : "0"+date.getUTCMilliseconds()) : date.getUTCMilliseconds())
Comment on lines +82 to +85
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, avoid this code style, if-else is more readable than multiple conditional operators ? :

And space around operators

}


@@ -94,7 +100,11 @@ function timeSpanToString(startDate, endDate) {
* Date.UTC(2016,3,5,21, 0) => Math.PI/2
*/
function angleBetweenClockHands(date) {
throw new Error('Not implemented');
let hoursDegrees = (date.getUTCHours()>=12) ? date.getUTCHours()%12 : date.getUTCHours();
hoursDegrees = Math.abs(0.5*(60*hoursDegrees-11*date.getUTCMinutes()))
if (hoursDegrees>180) hoursDegrees=360-hoursDegrees
let degrees = hoursDegrees*(Math.PI/180)
return degrees
}


Loading