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

Added my solutions to all tasks #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion module-1/classification.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ function grade(score) {
* Also take into consideration the documentation of the function!
*/
// PLACE YOUR CODE BETWEEN THIS...

if (score > 100 || score < 0 || typeof score !== 'number' ) gradeOfStudent = 0;
/*else if (score >= 90) gradeOfStudent = 5;
else if (score >= 80) gradeOfStudent = 4;
else if (score >= 70) gradeOfStudent = 3;
else if (score >= 60) gradeOfStudent = 2;
else gradeOfStudent = 1;*/
else gradeOfStudent = Math.floor(Math.min(99, (Math.max(59, score)))/10 - 4);
// ...AND THIS COMMENT LINE!
return gradeOfStudent;
}

module.exports = grade;
11 changes: 9 additions & 2 deletions module-1/euclidean.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ function euclidean(a, b) {
* Also take into consideration the documentation of the function!
*/
// PLACE YOUR CODE BETWEEN THIS...


if (a > 0 && b > 0 && typeof a === 'number' && typeof b === 'number') {
let remainder = a % b;
while (remainder > 0) {
a = b;
b = remainder;
remainder = a % b;
}
gcd = b;
} else gcd = 0;
// ...AND THIS COMMENT LINE!
return gcd;
}
Expand Down
14 changes: 14 additions & 0 deletions module-1/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@ function fibonacci(n) {
* Also take into consideration the documentation of the function!
*/
// PLACE YOUR CODE BETWEEN THIS...
if (n > 0 && typeof n === 'number') {
let a = 1;
let b = 1;
for (let i = 3; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
nThFibonacci = b;

/*if (n < 2) nThFibonacci = n; // recursive solution
else nThFibonacci = fibonacci(n-1) + fibonacci(n-2);*/

} else nThFibonacci = 0;

// ...AND THIS COMMENT LINE!
return nThFibonacci;
Expand Down
5 changes: 5 additions & 0 deletions module-1/test/classification.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ describe('Module 1 - Classification', () => {
return expect(classification(101)).to.equal(0);
});

it("should return 0 if score is not a number", () => {
return (expect(classification('word')).to.equal(0) &&
expect(classification(true)).to.equal(0));
});

const scores = [
[0, 1], [58, 1], [59, 1],
[60, 2], [61, 2], [68, 2], [69, 2],
Expand Down
8 changes: 7 additions & 1 deletion module-1/test/euclidean.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ describe('Module 1 - Euclidean algorithm', () => {
});

it("should return 0 if negative value passed", () => {
return expect(euclidean(-2, 2)).to.equal(0);
return (expect(euclidean(-2, 2)).to.equal(0) &&
expect(euclidean(3, -2)).to.equal(0));
});

it("should return 0 if passed value is not a number", () => {
return (expect(euclidean('word', 2)).to.equal(0) &&
expect(euclidean(3, false)).to.equal(0));
});

const values = [
Expand Down
5 changes: 5 additions & 0 deletions module-1/test/fibonacci.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ describe('Module 1 - Fibonacci', () => {
return expect(fibonacci(-2)).to.equal(0);
});

it("should return 0 if passed value is not a number", () => {
return (expect(fibonacci('-2')).to.equal(0) &&
expect(fibonacci(false)).to.equal(0));
});

const values = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377];
values.forEach((n, i) => {
it(`should return ${n} for ${i + 1}`, () => {
Expand Down
214 changes: 212 additions & 2 deletions module-2/test/calc.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,216 @@ describe.only('calc', () => {
* .minus(3)
* .times(6).v // 24
*/
// TODO: write test cases to test calculator

describe("Calculator foundations", () => {
it ("Calculator function exists", () => {
expect(calc()).to.not.be.undefined;
});
it ("First argument output method exists", () => {
expect(calc().v).to.not.be.undefined;
});
it ("Outputs first argument", () => {
expect(calc(3).v).to.equal(3);
});
it ("Handles absence of first argument", () => {
expect(calc().v).to.be.NaN;
});
it ("Handles invalid first argument", () => {
expect(calc('LOL').v).to.be.NaN;
expect(calc(true).v).to.be.NaN;
});
});
describe("Addition", () => {
it("Addition method exists", () => {
expect(calc().add()).to.not.be.undefined;
});
it ("Add two positive integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).add(ints[1]).v).to.equal(ints[0] + ints[1]);
});
it ("Add two negative integers", () => {
let ints = [-Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).add(ints[1]).v).to.equal(ints[0] + ints[1]);
});
it ("Add a positive and a negative integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).add(ints[1]).v).to.equal(ints[0] + ints[1]);
});
it ("Add two decimal numbers", () => {
let decs = [Math.round((Math.random() * 102 + 2) *100) / 100, Math.round((Math.random() * 102 + 2) *100) / 100];
expect(calc(decs[0]).add(decs[1]).v).to.equal(decs[0] + decs[1]);
});
it ("Addition handles absence of second argument", () => {
let int = [Math.round(Math.random() * 102 + 2)];
expect(calc(int).add().v).to.be.NaN;
});
it ("Addition handles invalid second argument", () => {
let int = [Math.round(Math.random() * 102 + 2)];
expect(calc(int).add('Ororo').v).to.be.NaN;
});
});
describe("Subtraction", () => {
it("Subtraction method exists", () => {
expect(calc().minus()).to.not.be.undefined;
});
it ("Subtract using two positive integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).minus(ints[1]).v).to.equal(ints[0] - ints[1]);
});
it ("Subtract using two negative integers", () => {
let ints = [-Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).minus(ints[1]).v).to.equal(ints[0] - ints[1]);
});
it ("Subtract using a positive and a negative integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).minus(ints[1]).v).to.equal(ints[0] - ints[1]);
});
it ("Subtract using two decimal numbers", () => {
let decs = [Math.round((Math.random() * 102 + 2) *100) / 100, Math.round((Math.random() * 102 + 2) *100) / 100];
expect(calc(decs[0]).minus(decs[1]).v).to.equal(decs[0] - decs[1]);
});
it ("Subtraction handles absence of second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).minus().v).to.be.NaN;
});
it ("Subtraction handles invalid second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).minus('Nope').v).to.be.NaN;
expect(calc(int).minus(true).v).to.be.NaN;
});
});
describe("Multiplication", () => {
it("Division method exists", () => {
expect(calc().times()).to.not.be.undefined;
});
it ("Multiply two positive integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).times(ints[1]).v).to.equal(ints[0] * ints[1]);
});
it ("Multiply two negative integers", () => {
let ints = [-Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).times(ints[1]).v).to.equal(ints[0] * ints[1]);
});
it ("Multiply a positive and a negative integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).times(ints[1]).v).to.equal(ints[0] * ints[1]);
});
it ("Multiply two decimal numbers", () => {
let decs = [Math.round((Math.random() * 102 + 2) *100) / 100, Math.round((Math.random() * 102 + 2) *100) / 100];
expect(calc(decs[0]).times(decs[1]).v).to.equal(decs[0] * decs[1]);
});
it ("Multiply by one", () => {
let ints = [Math.round(Math.random() * 102 + 2), 1];
expect(calc(ints[0]).times(ints[1]).v).to.equal(ints[0]);
});
it ("Multiply by zero", () => {
let ints = [Math.round(Math.random() * 102 + 2), 0];
expect(calc(ints[0]).times(ints[1]).v).to.equal(0);
});
it ("Multiplication handles absence of second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).times().v).to.be.NaN;
});
it ("Multiplication handles invalid second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).times('fg').v).to.be.NaN;
expect(calc(int).times(true).v).to.be.NaN;
});
});
describe("Division", () => {
it("Division method exists", () => {
expect(calc().divide()).to.not.be.undefined;
});
it ("Division using two positive integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).divide(ints[1]).v).to.equal(ints[0] / ints[1]);
});
it ("Division using two negative integers", () => {
let ints = [-Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).divide(ints[1]).v).to.equal(ints[0] / ints[1]);
});
it ("Division using a positive and a negative integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).divide(ints[1]).v).to.equal(ints[0] / ints[1]);
});
it ("Division using two decimal numbers", () => {
let decs = [Math.round((Math.random() * 102 + 2) *100) / 100, Math.round((Math.random() * 102 + 2) *100) / 100];
expect(calc(decs[0]).divide(decs[1]).v).to.equal(decs[0] / decs[1]);
});
it ("Division by itself", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).divide(int).v).to.equal(1);
});
it ("Division handles absence of second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).divide().v).to.be.NaN;
});
it ("Division handles invalid second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).divide('asdf').v).to.be.NaN;
expect(calc(int).divide(true).v).to.be.NaN;
});
it ("Handles division by zero", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(() => calc(int).divide(0)).to.throw('Division by 0 is not possible!');
});
});
describe("Square root", () => {
it("Root method exists", () => {
expect(calc().sqrt()).to.not.be.undefined;
});
it ("Root of an integer", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).sqrt().v).to.equal(Math.sqrt(int));
});
it ("Root of a decimal number", () => {
let dec = Math.round((Math.random() * 102 + 2) *100) / 100;
expect(calc(dec).sqrt().v).to.equal(Math.sqrt(dec));
});
it ("Root of one", () => {
let dec = 1;
expect(calc(dec).sqrt().v).to.equal(1);
});
it ("Root of zero", () => {
let dec = 0;
expect(calc(dec).sqrt().v).to.equal(0);
});
it ("Root handles negative number as argument", () => {
let int = -Math.round(Math.random() * 102 + 2);
expect(() => calc(int).sqrt()).to.throw('Square root of negative value cannot be determined!');
});
});
describe("Remainder", () => {
it("Remainder method exists", () => {
expect(calc().modulo()).to.not.be.undefined;
});
it ("Remainder for two positive integers", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).modulo(ints[1]).v).to.equal(ints[0] % ints[1]);
});
it ("Remainder for two negative integers", () => {
let ints = [-Math.round(Math.random() * 102 + 2), -Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0]).modulo(ints[1]).v).to.equal(ints[0] % ints[1]);
});
it ("Remainder for two equal numbers", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).modulo(int).v).to.equal(0);
});
it ("No remainder (complete division)", () => {
let ints = [Math.round(Math.random() * 102 + 2), Math.round(Math.random() * 102 + 2)];
expect(calc(ints[0] * ints[1]).modulo(ints[0]).v).to.equal(0);
});
it ("Remainder handles division by zero", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).modulo(0).v).to.be.NaN;
});
it ("Remainder handles absence of second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).modulo().v).to.be.NaN;
});
it ("Remainder handles invalid second argument", () => {
let int = Math.round(Math.random() * 102 + 2);
expect(calc(int).modulo('qwe12').v).to.be.NaN;
expect(calc(int).modulo(true).v).to.be.NaN;
});
});
});
58 changes: 54 additions & 4 deletions module-3/pop/Element.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,68 @@
/**
* Create Element class, which represents an element of
* the application, and
*
*
* 1. It has a protractor locator (.locator),
* e.g. by.css("h1.title")
* 2. It has a name (.name), e.g. "Document Title"
* 3. It can have a parent Element,
* 3. It can have a parent Element,
* which is the context of the element (.parent)
* 4. It can have children Elements (.children)
* 5. It has a method to retrieve the protractor element
* by the locator (.get([name])) in it's context
* - if it gets a name as parameter, it tries to find
* in it's children (recursively) the Element with
* the given name or throws an Erorr if it cannot
* the given name or throws an Error if it cannot
* find the element
*/
module.exports = class Element {}
const ElementFinder = require('../test/mock/ElementFinder');

class Element {
constructor(name, locator) {
this.locator = locator;
this.name = name;
this.parent = null;
this.children = {};
}

setParent(parent) {
this.parent = parent;
}

addChildren(child) {
if (this.children.hasOwnProperty(child.name)) {
throw new Error('Child with \'' + child.name + '\' name already exists!')
} else {
this.children[child.name] = child;
}
}

get(name) {
let result = [];

function getThat(where) {
if (name === undefined) {
result.push(new ElementFinder(where.locator));
} else if (where.children !== null) {
if (Object.keys(where.children).length) {
if (where.children.hasOwnProperty(name)) {
result.push(new ElementFinder(where.children[name].locator));
} else {
for (let key in where.children) {
getThat(where.children[key]);
}
}
}
}
}

getThat(this);
if (result.length === 0) {
throw new Error(`No child with ${name} name exists!`);
} else {
return result[0];
}
}
}

module.exports = Element;
Loading