Skip to content

Commit a45bb93

Browse files
committed
Fixed tests
1 parent 4f2bf4b commit a45bb93

3 files changed

Lines changed: 122 additions & 54 deletions

File tree

chapter1/index.js

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,87 @@
1-
(function(){
1+
let Formulas = {};
22

3-
console.log('Hello world');
3+
Formulas.add = function(a, b){
4+
return a + b;
5+
}
46

5-
let add = function(a, b){
6-
return a + b;
7+
Formulas.divideMultiply = function(a, b){
8+
if(b==0){
9+
return undefined;
710
}
11+
return a * a / b;
12+
}
813

9-
let o = {
10-
key: { key: { key: 42 }}
11-
};
14+
Formulas.roundNumbers = function(a){
15+
return Math.round(a);
16+
}
1217

13-
let arr = [1,2,3,4,5];
14-
15-
console.log(arr[4])
18+
Formulas.sinTimesCos = function(a){
19+
return Math.sin(a)*Math.cos(a);
20+
}
1621

22+
Formulas.parseNumberFromString = function(a){
23+
return parseFloat(a);
24+
}
1725

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){
32+
return Object.values(obj)[i];
33+
}
34+
if(typeof Object.values(obj)[i] === "object" && (Object.values(obj)[i] !== null)){
35+
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+
}
46+
}
47+
}
48+
}
49+
}
50+
}
51+
}
52+
53+
Formulas.parseJSON = function(a){
54+
return JSON.parse(a);
55+
}
1856

57+
Formulas.pushToArray = function(a,b){
58+
return a.push(b);
59+
}
1960

20-
let blablaFunction = function(phrase) {
21-
return function(){
22-
console.log('Phrase ' + phrase);
23-
}
61+
Formulas.squareArrayValues = function(a){
62+
var newArr = a;
63+
for (var i = 0; i < newArr.length; i++){
64+
newArr[i] = newArr[i]*newArr[i];
2465
}
66+
return a;
67+
}
68+
69+
Formulas.sortArray = function(a){
70+
return a.sort(Formulas.sortNumbers)
71+
}
2572

26-
let composedBlablaFunction = function(blaFunction) {
27-
console.log('Get some air in your lungs!');
28-
blaFunction();
29-
console.log('Now you can rest!')
73+
Formulas.sortNumbers = function(a,b){
74+
return a-b;
75+
}
76+
77+
Formulas.reverseString = function(a){
78+
var newString = "";
79+
for (var i = a.length - 1; i >= 0; i--) {
80+
newString += a[i];
3081
}
82+
return newString;
83+
}
84+
85+
module.exports = Formulas;
86+
3187

32-
composedBlablaFunction(blablaFunction('Hello world'))
33-
}());

chapter1/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "./node_modules/mocha/bin/mocha"
7+
"test": "mocha"
88
},
99
"author": "",
1010
"license": "MIT",

chapter1/test/index.test.js

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,99 @@
11
var assert = require('assert');
2+
var Formulas = require('../index.js');
23

34
describe('JS Basics', function() {
45
describe('Numbers', function() {
5-
it('should be able to add number', function() {
6-
var a = 42.9902822;
7-
var b = 43.2929112;
8-
assert.equal(a + b, 99);
6+
it('should be able to add numbers', function() {
7+
var a = 45;
8+
var b = 54;
9+
assert.equal(Formulas.add(a,b), 99);
910
});
1011

1112
it('should be able to divide and multiply number', function() {
12-
var a = 42.94;
13-
var b = 0;
13+
var a = 4;
14+
var b = 2;
15+
assert.equal(Formulas.divideMultiply(a,b), 8);
16+
});
1417

15-
assert.equal(a * a / b, 42);
18+
it('should return undefined if divided by 0', function() {
19+
var a = 4;
20+
var b = 0;
21+
assert.equal(Formulas.divideMultiply(a,b), undefined);
1622
});
1723

1824
it('should be able to round numbers', function() {
1925
var a = 42.94;
20-
assert.equal(a, 42);
26+
assert.equal(Formulas.roundNumbers(a), 43);
2127
});
2228

23-
it('should be able to find sin(x) * cos(x)', function() {
24-
var a = 42;
25-
assert.equal(a, 1);
29+
it('should be able to find sin(x)*cos(x)', function() {
30+
var a = 0;
31+
assert.equal(Formulas.sinTimesCos(a), 0);
2632
});
2733

2834
it('should be able to parse number form string', function() {
2935
var price = "9.99 $"
30-
assert.equal(price, 9.99);
36+
assert.equal(Formulas.parseNumberFromString(price), 9.99);
3137
});
3238
});
3339

3440

3541
describe('Objects', function() {
36-
it('should be find object value by key', function() {
37-
var obj = { a: {b: { d: "foo" }}, c: 42 }
38-
assert.equal(obj, "foo");
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");
3945
});
4046

41-
it('should be find object value by dynamic key', function() {
42-
var obj = { a: {b: { d: "foo" }}, c: 42 }
43-
assert.equal(obj["hm"], 42);
44-
});
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+
});
4551

46-
it('should be parse object from json', function() {
52+
it('should be able to parse object from json', function() {
4753
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}]}]}'
48-
var dateStart = 42;
54+
var parsedJSON = Formulas.parseJSON(json);
55+
var dateStart = parsedJSON.user_lessons[0].date_start;
4956
assert.equal(dateStart, 1533108640);
5057
});
5158

52-
it('should be set objet key', function() {
59+
it('should be able to set object key', function() {
5360
var obj = { a: {b: { d: "foo" }}, c: 42 }
61+
obj.a.b = "Js Rocks!";
5462
assert.equal(obj.a.b, "Js Rocks!")
5563
});
5664
});
5765

5866

5967
describe('Arrays', function() {
60-
it('should be access array by index', function() {
61-
var arrray = [1,2,3,4,5,6,7,8,9]
62-
assert.equal(arrray, 5);
68+
it('should be able to access array by index', function() {
69+
var arrray = [1,2,3,4,5,6,7,8,9];
70+
assert.equal(arrray[4], 5);
6371
});
6472

6573
it('should to push and pop from array', function() {
66-
var arrray = [1,2,3,4,5,6,7,8,9]
67-
assert.equal(arrray.length, 10);
74+
var array = [1,2,3,4,5,6,7,8,9];
75+
Formulas.pushToArray(array, 10);
76+
assert.equal(array.length, 10);
6877
});
6978

7079
it('should be able to output square of array values', function() {
71-
assert.equal()
80+
var arr = [1,2,3,4,5];
81+
var expectedArr = [1,4,9,16,25];
82+
var newArr = Formulas.squareArrayValues(arr);
83+
assert.deepEqual(newArr, expectedArr);
7284
});
7385

7486
it('should be able to sort array', function() {
75-
var arr = [23,23,4,5,123,7,32,13,13,9]
76-
assert.equal(arr, []);
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];
89+
var sortedArr = Formulas.sortArray(arr);
90+
assert.deepEqual(sortedArr, expectedArr);
7791
});
7892

7993
it('should be able to reverse string', function() {
80-
81-
var string = "I love corgies!"
82-
assert.equal(string, "!seigroc evol I")
94+
var string = "I love corgies!";
95+
var newString = Formulas.reverseString(string);
96+
assert.equal(newString, "!seigroc evol I");
8397
});
8498
});
8599
});

0 commit comments

Comments
 (0)