Skip to content

Commit 811f3b4

Browse files
committed
added some changes to initial tests and created bank-account file
1 parent a45bb93 commit 811f3b4

File tree

4 files changed

+103
-73
lines changed

4 files changed

+103
-73
lines changed

chapter1/bank-account.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
var BankSecurity = {
2+
foo: 42
3+
}
4+
5+
var BankAccount = {
6+
balance: 0,
7+
8+
deposit: function (money) {
9+
this.balance = this.balance + money;
10+
},
11+
12+
widthraw: function (cash) {
13+
this.balance = this.balance - cash;
14+
return this.balance;
15+
}
16+
}
17+
18+
module.exports = {
19+
BankAccount: BankAccount,
20+
BankSecurity: BankSecurity
21+
}

chapter1/index.js

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,81 @@
11
let Formulas = {};
22

3-
Formulas.add = function(a, b){
3+
Formulas.add = function (a, b) {
44
return a + b;
55
}
66

7-
Formulas.divideMultiply = function(a, b){
8-
if(b==0){
7+
Formulas.divideMultiply = function (a, b) {
8+
if (b == 0) {
99
return undefined;
1010
}
1111
return a * a / b;
1212
}
1313

14-
Formulas.roundNumbers = function(a){
14+
Formulas.roundNumbers = function (a) {
1515
return Math.round(a);
1616
}
1717

18-
Formulas.sinTimesCos = function(a){
19-
return Math.sin(a)*Math.cos(a);
18+
Formulas.sinTimesCos = function (a) {
19+
return Math.sin(a) * Math.cos(a);
2020
}
2121

22-
Formulas.parseNumberFromString = function(a){
22+
Formulas.parseNumberFromString = function (a) {
2323
return parseFloat(a);
2424
}
2525

26-
Formulas.findObjectByKey = function(obj, key){
27-
var i;
28-
var j;
29-
var p;
30-
for(i = 0; i < Object.keys(obj).length; i++){
31-
if(Object.keys(obj)[i] === key){
26+
Formulas.findObjectValueByKey = function (obj, key) {
27+
let i;
28+
let j;
29+
let p;
30+
31+
for (i = 0; i < Object.keys(obj).length; i++) {
32+
if (Object.keys(obj)[i] === key) {
3233
return Object.values(obj)[i];
3334
}
34-
if(typeof Object.values(obj)[i] === "object" && (Object.values(obj)[i] !== null)){
35+
if (typeof Object.values(obj)[i] === "object" && (Object.values(obj)[i] !== null)) {
3536
var newObject = Object.values(obj)[i];
36-
for(j = 0; j < Object.keys(newObject).length; j++){
37-
if(Object.keys(newObject)[j] === key){
38-
return Object.values(newObject)[j];
39-
}
40-
if(typeof Object.values(newObject)[j] === "object" && (Object.values(newObject)[j] !== null)){
41-
var newObject2 = Object.values(newObject)[j];
42-
for(p = 0; p < Object.keys(newObject2).length; p++){
43-
if(Object.keys(newObject2)[p] === key){
44-
return Object.values(newObject2)[p];
45-
}
37+
for (j = 0; j < Object.keys(newObject).length; j++) {
38+
if (Object.keys(newObject)[j] === key) {
39+
return Object.values(newObject)[j];
40+
}
41+
if (typeof Object.values(newObject)[j] === "object" && (Object.values(newObject)[j] !== null)) {
42+
var newObject2 = Object.values(newObject)[j];
43+
for (p = 0; p < Object.keys(newObject2).length; p++) {
44+
if (Object.keys(newObject2)[p] === key) {
45+
return Object.values(newObject2)[p];
4646
}
4747
}
48-
}
48+
}
49+
}
4950
}
5051
}
5152
}
5253

53-
Formulas.parseJSON = function(a){
54+
Formulas.parseJSON = function (a) {
5455
return JSON.parse(a);
5556
}
5657

57-
Formulas.pushToArray = function(a,b){
58+
Formulas.pushToArray = function (a, b) {
5859
return a.push(b);
5960
}
6061

61-
Formulas.squareArrayValues = function(a){
62+
Formulas.squareArrayValues = function (a) {
6263
var newArr = a;
63-
for (var i = 0; i < newArr.length; i++){
64-
newArr[i] = newArr[i]*newArr[i];
64+
for (var i = 0; i < newArr.length; i++) {
65+
newArr[i] = newArr[i] * newArr[i];
6566
}
6667
return a;
6768
}
6869

69-
Formulas.sortArray = function(a){
70+
Formulas.sortArray = function (a) {
7071
return a.sort(Formulas.sortNumbers)
7172
}
7273

73-
Formulas.sortNumbers = function(a,b){
74-
return a-b;
74+
Formulas.sortNumbers = function (a, b) {
75+
return a - b;
7576
}
7677

77-
Formulas.reverseString = function(a){
78+
Formulas.reverseString = function (a) {
7879
var newString = "";
7980
for (var i = a.length - 1; i >= 0; i--) {
8081
newString += a[i];

chapter1/test/bank-account.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var assert = require('assert');
2+
var BankAccount = require('../bank-account').BankAccount;
3+
4+
describe('Bank account', () => {
5+
it('should have no money on deposit in the beginning', () => {
6+
assert.equal(BankAccount.balance, 0);
7+
});
8+
})

chapter1/test/index.test.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,97 @@
11
var assert = require('assert');
22
var Formulas = require('../index.js');
33

4-
describe('JS Basics', function() {
5-
describe('Numbers', function() {
6-
it('should be able to add numbers', function() {
7-
var a = 45;
4+
describe('JS Basics', function () {
5+
describe('Numbers', function () {
6+
it('should be able to add numbers', function () {
7+
var a = 45;
88
var b = 54;
9-
assert.equal(Formulas.add(a,b), 99);
9+
assert.equal(Formulas.add(a, b), 99);
1010
});
1111

12-
it('should be able to divide and multiply number', function() {
13-
var a = 4;
12+
it('should be able to divide and multiply number', function () {
13+
var a = 4;
1414
var b = 2;
15-
assert.equal(Formulas.divideMultiply(a,b), 8);
15+
assert.equal(Formulas.divideMultiply(a, b), 8);
1616
});
1717

18-
it('should return undefined if divided by 0', function() {
19-
var a = 4;
18+
it('should return undefined if divided by 0', function () {
19+
var a = 4;
2020
var b = 0;
21-
assert.equal(Formulas.divideMultiply(a,b), undefined);
21+
assert.equal(Formulas.divideMultiply(a, b), undefined);
2222
});
2323

24-
it('should be able to round numbers', function() {
25-
var a = 42.94;
24+
it('should be able to round numbers', function () {
25+
var a = 42.94;
2626
assert.equal(Formulas.roundNumbers(a), 43);
2727
});
2828

29-
it('should be able to find sin(x)*cos(x)', function() {
29+
it('should be able to find sin(x)*cos(x)', function () {
3030
var a = 0;
3131
assert.equal(Formulas.sinTimesCos(a), 0);
3232
});
3333

34-
it('should be able to parse number form string', function() {
34+
it('should be able to parse number form string', function () {
3535
var price = "9.99 $"
3636
assert.equal(Formulas.parseNumberFromString(price), 9.99);
3737
});
3838
});
3939

4040

41-
describe('Objects', function() {
42-
it('should be able to find object value by key', function() {
43-
var obj = { a: {b: { d: "foo" }}, c: 42 };
44-
assert.equal(Formulas.findObjectByKey(obj,'d'), "foo");
41+
describe('Objects', function () {
42+
it('should be able to find object value by key', function () {
43+
var obj = { a: { b: { d: "foo" } }, c: 42 };
44+
assert.equal(Formulas.findObjectValueByKey(obj, 'd'), "foo");
4545
});
4646

47-
it('should be able to find object value by dynamic key', function() {
48-
var obj = {a: {b: { d: "foo" }}, c: 42 }
49-
assert.equal(Formulas.findObjectByKey(obj,'c'), 42);;
50-
});
47+
it('should be able to find object value by dynamic key', function () {
48+
var obj = { a: { b: { d: "foo" } }, c: 42 }
49+
assert.equal(Formulas.findObjectValueByKey(obj, 'c'), 42);;
50+
});
5151

52-
it('should be able to parse object from json', function() {
52+
it('should be able to parse object from json', function () {
5353
var json = '{"ok":true,"user_lessons":[{"user_lesson_id":408097171313,"state":"completed","skip":false,"lesson_id":1,"date_start":1533108640,"tasks":[{"user_task_id":407936828624,"state":"skipped","current_step":"","task_id":1},{"user_task_id":408791535509,"state":"skipped","current_step":"","task_id":2},{"user_task_id":409970847238,"state":"skipped","current_step":"","task_id":3}]}]}'
5454
var parsedJSON = Formulas.parseJSON(json);
5555
var dateStart = parsedJSON.user_lessons[0].date_start;
5656
assert.equal(dateStart, 1533108640);
5757
});
5858

59-
it('should be able to set object key', function() {
60-
var obj = { a: {b: { d: "foo" }}, c: 42 }
59+
it('should be able to set object key', function () {
60+
var obj = { a: { b: { d: "foo" } }, c: 42 }
6161
obj.a.b = "Js Rocks!";
6262
assert.equal(obj.a.b, "Js Rocks!")
6363
});
6464
});
6565

6666

67-
describe('Arrays', function() {
68-
it('should be able to access array by index', function() {
69-
var arrray = [1,2,3,4,5,6,7,8,9];
67+
describe('Arrays', function () {
68+
it('should be able to access array by index', function () {
69+
var arrray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
7070
assert.equal(arrray[4], 5);
7171
});
7272

73-
it('should to push and pop from array', function() {
74-
var array = [1,2,3,4,5,6,7,8,9];
73+
it('should to push and pop from array', function () {
74+
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
7575
Formulas.pushToArray(array, 10);
7676
assert.equal(array.length, 10);
7777
});
7878

79-
it('should be able to output square of array values', function() {
80-
var arr = [1,2,3,4,5];
81-
var expectedArr = [1,4,9,16,25];
79+
it('should be able to output square of array values', function () {
80+
var arr = [1, 2, 3, 4, 5];
81+
var expectedArr = [1, 4, 9, 16, 25];
8282
var newArr = Formulas.squareArrayValues(arr);
8383
assert.deepEqual(newArr, expectedArr);
8484
});
8585

86-
it('should be able to sort array', function() {
87-
var arr = [23,23,4,5,123,7,32,13,13,9];
88-
var expectedArr = [4,5,7,9,13,13,23,23,32,123];
86+
it('should be able to sort array', function () {
87+
var arr = [23, 23, 4, 5, 123, 7, 32, 13, 13, 9];
88+
var expectedArr = [4, 5, 7, 9, 13, 13, 23, 23, 32, 123];
8989
var sortedArr = Formulas.sortArray(arr);
9090
assert.deepEqual(sortedArr, expectedArr);
9191
});
9292

93-
it('should be able to reverse string', function() {
94-
var string = "I love corgies!";
93+
it('should be able to reverse string', function () {
94+
var string = "I love corgies!";
9595
var newString = Formulas.reverseString(string);
9696
assert.equal(newString, "!seigroc evol I");
9797
});

0 commit comments

Comments
 (0)