diff --git a/module-1/classification.js b/module-1/classification.js index 9f85b22..0a78d9d 100644 --- a/module-1/classification.js +++ b/module-1/classification.js @@ -22,7 +22,23 @@ function grade(score) { */ // PLACE YOUR CODE BETWEEN THIS... - // ...AND THIS COMMENT LINE! + if (score < 0 || score > 100) { + gradeOfStudent = 0; + } + else { + gradeOfStudent = Math.ceil(Math.max(((score - 59) / 41) * 4, 0)) + 1; + } + + // ...AND THIS COMMENT LINE! + return gradeOfStudent; } -module.exports = grade; \ No newline at end of file + + + + + + + + +module.exports = grade; diff --git a/module-1/euclidean.js b/module-1/euclidean.js index 3d33d00..25b782a 100644 --- a/module-1/euclidean.js +++ b/module-1/euclidean.js @@ -16,9 +16,21 @@ function euclidean(a, b) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... - + if (a > 0 && b > 0) { + while (a !== b) { + if (a > b) { + a = a - b; + } else { + b = b - a; + } + } + gcd = a; + } else { + gcd = 0; + } // ...AND THIS COMMENT LINE! return gcd; } -module.exports = euclidean; \ No newline at end of file +module.exports = euclidean; + diff --git a/module-1/fibonacci.js b/module-1/fibonacci.js index 14ec907..0aa904f 100644 --- a/module-1/fibonacci.js +++ b/module-1/fibonacci.js @@ -15,8 +15,25 @@ function fibonacci(n) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... - + if (n >= 0) { + if (n < 2) { + nThFibonacci = n; + } else { + let f0 = 0; + let f1 = 1; + for (let i = 2; i <= n; ++i) { + const sum = f0 + f1; + f0 = f1; + f1 = sum; + } + nThFibonacci = f1; + } + } else { + nThFibonacci = 0; + } // ...AND THIS COMMENT LINE! return nThFibonacci; } -module.exports = fibonacci; \ No newline at end of file + +module.exports = fibonacci; + diff --git a/module-2/test/calc.spec.js b/module-2/test/calc.spec.js index c6fc7c7..6907f6f 100644 --- a/module-2/test/calc.spec.js +++ b/module-2/test/calc.spec.js @@ -1,3 +1,4 @@ +const { Then } = require('cucumber'); const calc = require('../calc'); const expect = require('chai').expect; @@ -17,5 +18,144 @@ describe.only('calc', () => { * .times(6).v // 24 */ // TODO: write test cases to test calculator + it("should have proper value", () => { + //Given + const c = calc(3); + //When + //Then + expect(c.v).to.equal(3); + }); -}); \ No newline at end of file + describe("add", () => { + it("should exist", () => { + //Given + const c = calc(3); + //When + //Then + expect(c.add).not.to.undefined; + }); + it("should be able to add a number to the current value", () => { + //Given + const c = calc(3); + //When + const result = calc(3).add(5); + //Then + expect(result.v).to.equal(8); + }); + }); + + describe("minus", () => { + it("should exist", () => { + //Given + const c = calc(3); + //When + //Then + expect(c.minus).not.to.undefined; + }); + it("should be able to subtract a number from the current value", () => { + //Given + const c = calc(3); + //When + const result = calc(3).minus(2); + //Then + expect(result.v).to.equal(1); + }); + }); + + describe("divide", () => { + it("should exist", () => { + //Given + const c = calc(42); + //When + //THen + expect(c.divide).not.to.undefined; + }); + it("should be able to perform a valid division", () => { + //Given + const c = calc(10); + //When + const result = c.divide(2).v; + //Then + expect(result).to.be.equal(5); + }); + it("should handle division by 0", () => { + //Given + const c = calc(5); + //When + //Then + expect(() => c.divide(0)).to.throw("Division by 0 is not possible!"); + }); + }); + + describe("sqrt", () => { + it("should exist", () => { + //Given + const c = calc(4); + //When + //THen + expect(c.sqrt).not.to.undefined; + }); + it("should be able to square a number", () => { + //Given + const c = calc(4); + //When + const result = calc(4).sqrt().v; + //Then + expect(result).to.equal(2); + }); + it ("should handle negative numbers", () => { + //Given + const c = calc(-3); + //When + //Then + expect(() => c.sqrt()).to.throw("Square root of negative value cannot be determined!"); + }); + }); + + describe("times", () => { + it("should exist", () => { + //Given + const c = calc(4); + //When + //THen + expect(c.times).not.to.undefined; + }); + it("should be able to multiply numbers", () => { + //Given + const c = calc(3); + //When + const result = calc(3).times(10).v; + //Then + expect(result).to.equal(30); + }); + }); + + describe("modulo", () => { + it("should exist", () => { + //Given + const c = calc(10); + //When + //THen + expect(c.modulo).not.to.undefined; + }); + it("should be able to perform modulo operation", () => { + //Given + const c = calc(10); + //When + const result = c.modulo(5).v; + //Then + expect(result).to.equal(0); + }); + }); + + describe("multiply operations", () => { + it("should be able to perform multiply operations", () => { + //Given + const c = calc(3); + //When + const result = c.add(4).minus(3).times(6).v; + //THen + expect(result).to.equal(24); + }); + }); +});