Skip to content

Test fix #2

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
94 changes: 74 additions & 20 deletions chapter1/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,87 @@
(function(){
let Formulas = {};

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

let add = function(a, b){
return a + b;
Formulas.divideMultiply = function(a, b){
if(b==0){
return undefined;
}
return a * a / b;
}

let o = {
key: { key: { key: 42 }}
};
Formulas.roundNumbers = function(a){
return Math.round(a);
}

let arr = [1,2,3,4,5];

console.log(arr[4])
Formulas.sinTimesCos = function(a){
return Math.sin(a)*Math.cos(a);
Copy link
Owner

Choose a reason for hiding this comment

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

Spacing here. Watch out for good programming style.

}

Formulas.parseNumberFromString = function(a){
return parseFloat(a);
}

Formulas.findObjectByKey = function(obj, key){
var i;
var j;
var p;
for(i = 0; i < Object.keys(obj).length; i++){
Copy link
Owner

Choose a reason for hiding this comment

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

you could use let instead of var here. It behaves more like normal variable from Java.

Copy link
Owner

Choose a reason for hiding this comment

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

You could extract Object.keys(obj) to a variable.

if(Object.keys(obj)[i] === key){
return Object.values(obj)[i];
}
if(typeof Object.values(obj)[i] === "object" && (Object.values(obj)[i] !== null)){
var newObject = Object.values(obj)[i];
for(j = 0; j < Object.keys(newObject).length; j++){
if(Object.keys(newObject)[j] === key){
return Object.values(newObject)[j];
}
if(typeof Object.values(newObject)[j] === "object" && (Object.values(newObject)[j] !== null)){
var newObject2 = Object.values(newObject)[j];
for(p = 0; p < Object.keys(newObject2).length; p++){
if(Object.keys(newObject2)[p] === key){
return Object.values(newObject2)[p];
}
}
}
}
}
}
}

Formulas.parseJSON = function(a){
return JSON.parse(a);
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe to reason to wrap this stuff in function.

}

Formulas.pushToArray = function(a,b){
return a.push(b);
Copy link
Owner

Choose a reason for hiding this comment

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

Here too.

}

let blablaFunction = function(phrase) {
return function(){
console.log('Phrase ' + phrase);
}
Formulas.squareArrayValues = function(a){
var newArr = a;
for (var i = 0; i < newArr.length; i++){
Copy link
Owner

Choose a reason for hiding this comment

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

  1. Why not iterate over a?
  2. Why not giving proper names like arrayToBeSquared

newArr[i] = newArr[i]*newArr[i];
}
return a;
}

Formulas.sortArray = function(a){
return a.sort(Formulas.sortNumbers)
Copy link
Owner

Choose a reason for hiding this comment

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

Not move one-liners to methods unless there is some specific logic there.

}

let composedBlablaFunction = function(blaFunction) {
console.log('Get some air in your lungs!');
blaFunction();
console.log('Now you can rest!')
Formulas.sortNumbers = function(a,b){
Copy link
Owner

Choose a reason for hiding this comment

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

This is a sortFunction that will be applied to the array.

return a-b;
}

Formulas.reverseString = function(a){
var newString = "";
for (var i = a.length - 1; i >= 0; i--) {
newString += a[i];
}
return newString;
}

module.exports = Formulas;


composedBlablaFunction(blablaFunction('Hello world'))
}());
24 changes: 12 additions & 12 deletions chapter1/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chapter1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/mocha/bin/mocha"
"test": "mocha"
},
"author": "",
"license": "MIT",
Expand Down
80 changes: 47 additions & 33 deletions chapter1/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,99 @@
var assert = require('assert');
var Formulas = require('../index.js');

describe('JS Basics', function() {
describe('Numbers', function() {
it('should be able to add number', function() {
var a = 42.9902822;
var b = 43.2929112;
assert.equal(a + b, 99);
it('should be able to add numbers', function() {
var a = 45;
var b = 54;
assert.equal(Formulas.add(a,b), 99);
});

it('should be able to divide and multiply number', function() {
var a = 42.94;
var b = 0;
var a = 4;
var b = 2;
assert.equal(Formulas.divideMultiply(a,b), 8);
});

assert.equal(a * a / b, 42);
it('should return undefined if divided by 0', function() {
var a = 4;
var b = 0;
assert.equal(Formulas.divideMultiply(a,b), undefined);
});

it('should be able to round numbers', function() {
var a = 42.94;
assert.equal(a, 42);
assert.equal(Formulas.roundNumbers(a), 43);
});

it('should be able to find sin(x) * cos(x)', function() {
var a = 42;
assert.equal(a, 1);
it('should be able to find sin(x)*cos(x)', function() {
var a = 0;
assert.equal(Formulas.sinTimesCos(a), 0);
});

it('should be able to parse number form string', function() {
var price = "9.99 $"
assert.equal(price, 9.99);
assert.equal(Formulas.parseNumberFromString(price), 9.99);
});
});


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

it('should be find object value by dynamic key', function() {
var obj = { a: {b: { d: "foo" }}, c: 42 }
assert.equal(obj["hm"], 42);
});
it('should be able to find object value by dynamic key', function() {
var obj = {a: {b: { d: "foo" }}, c: 42 }
assert.equal(Formulas.findObjectByKey(obj,'c'), 42);;
});

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

it('should be set objet key', function() {
it('should be able to set object key', function() {
var obj = { a: {b: { d: "foo" }}, c: 42 }
obj.a.b = "Js Rocks!";
assert.equal(obj.a.b, "Js Rocks!")
});
});


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

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

it('should be able to output square of array values', function() {
assert.equal()
var arr = [1,2,3,4,5];
var expectedArr = [1,4,9,16,25];
var newArr = Formulas.squareArrayValues(arr);
assert.deepEqual(newArr, expectedArr);
});

it('should be able to sort array', function() {
var arr = [23,23,4,5,123,7,32,13,13,9]
assert.equal(arr, []);
var arr = [23,23,4,5,123,7,32,13,13,9];
var expectedArr = [4,5,7,9,13,13,23,23,32,123];
var sortedArr = Formulas.sortArray(arr);
assert.deepEqual(sortedArr, expectedArr);
});

it('should be able to reverse string', function() {

var string = "I love corgies!"
assert.equal(string, "!seigroc evol I")
var string = "I love corgies!";
var newString = Formulas.reverseString(string);
assert.equal(newString, "!seigroc evol I");
});
});
});