From 6e721556744e33fef018a0ba8359eb5f907b7fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Bakosi?= Date: Mon, 23 Nov 2020 11:54:22 +0100 Subject: [PATCH 1/4] Module-1 task solutions --- module-1/classification.js | 27 +++++++++++++++++++++++++-- module-1/euclidean.js | 16 ++++++++++++++-- module-1/fibonacci.js | 29 +++++++++++++++++++++++++++-- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/module-1/classification.js b/module-1/classification.js index 9f85b22..46727f3 100644 --- a/module-1/classification.js +++ b/module-1/classification.js @@ -22,7 +22,30 @@ function grade(score) { */ // PLACE YOUR CODE BETWEEN THIS... - // ...AND THIS COMMENT LINE! + + if (score < 0 || score > 100) { + gradeOfStudent = 0; + } + else if (score >= 0 && score < 60) { + gradeOfStudent = 1; + } + else if (score >= 60 && score < 70) { + gradeOfStudent = 2; + } + else if (score >= 70 && score < 80) { + gradeOfStudent = 3; + } + else if (score >= 80 && score < 90) { + gradeOfStudent = 4; + } + else if (score >= 90 && score <= 100) { + gradeOfStudent = 5; + } + + // ...AND THIS COMMENT LINE! + return gradeOfStudent; } -module.exports = grade; \ No newline at end of file +module.exports = grade; + + \ No newline at end of file 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..3f297fb 100644 --- a/module-1/fibonacci.js +++ b/module-1/fibonacci.js @@ -15,8 +15,33 @@ function fibonacci(n) { * Also take into consideration the documentation of the function! */ // PLACE YOUR CODE BETWEEN THIS... + if (n >= 0) { - // ...AND THIS COMMENT LINE! + 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; + From fda9f99711ee700f260e945d2f2d78e31b571a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Bakosi?= Date: Mon, 23 Nov 2020 13:16:05 +0100 Subject: [PATCH 2/4] Module-2 task solution --- module-2/test/calc.spec.js | 168 ++++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 1 deletion(-) diff --git a/module-2/test/calc.spec.js b/module-2/test/calc.spec.js index c6fc7c7..73e5f77 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; @@ -18,4 +19,169 @@ describe.only('calc', () => { */ // TODO: write test cases to test calculator -}); \ No newline at end of file + it("should have proper value", () => { + //Given + const c = calc(3); + //When + //Then + expect(c.v).to.equal(3); + }); + + 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"); + + + }); + }); + 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"); + }); + }); + 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); + + }); + + }); + + + + +}); + From 70892dc4d64defb552cca4b9892819c1a94bc016 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Bakosi?= Date: Mon, 30 Nov 2020 09:14:20 +0100 Subject: [PATCH 3/4] Changes on module-1/classification.js, module-1/fibonacci.js and module-2/test/calc-spec.js based on Sandor Orosz's comments --- module-1/classification.js | 29 +++++++++++------------------ module-1/fibonacci.js | 12 ++---------- module-2/test/calc.spec.js | 38 ++++++-------------------------------- 3 files changed, 19 insertions(+), 60 deletions(-) diff --git a/module-1/classification.js b/module-1/classification.js index 46727f3..0a78d9d 100644 --- a/module-1/classification.js +++ b/module-1/classification.js @@ -22,30 +22,23 @@ function grade(score) { */ // PLACE YOUR CODE BETWEEN THIS... - if (score < 0 || score > 100) { gradeOfStudent = 0; } - else if (score >= 0 && score < 60) { - gradeOfStudent = 1; - } - else if (score >= 60 && score < 70) { - gradeOfStudent = 2; - } - else if (score >= 70 && score < 80) { - gradeOfStudent = 3; - } - else if (score >= 80 && score < 90) { - gradeOfStudent = 4; + else { + gradeOfStudent = Math.ceil(Math.max(((score - 59) / 41) * 4, 0)) + 1; } - else if (score >= 90 && score <= 100) { - gradeOfStudent = 5; - } - + // ...AND THIS COMMENT LINE! return gradeOfStudent; } -module.exports = grade; - \ No newline at end of file + + + + + + + +module.exports = grade; diff --git a/module-1/fibonacci.js b/module-1/fibonacci.js index 3f297fb..0aa904f 100644 --- a/module-1/fibonacci.js +++ b/module-1/fibonacci.js @@ -16,32 +16,24 @@ function fibonacci(n) { */ // 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! + // ...AND THIS COMMENT LINE! return nThFibonacci; } + module.exports = fibonacci; diff --git a/module-2/test/calc.spec.js b/module-2/test/calc.spec.js index 73e5f77..6907f6f 100644 --- a/module-2/test/calc.spec.js +++ b/module-2/test/calc.spec.js @@ -18,7 +18,6 @@ describe.only('calc', () => { * .times(6).v // 24 */ // TODO: write test cases to test calculator - it("should have proper value", () => { //Given const c = calc(3); @@ -35,7 +34,6 @@ describe.only('calc', () => { //Then expect(c.add).not.to.undefined; }); - it("should be able to add a number to the current value", () => { //Given const c = calc(3); @@ -43,10 +41,9 @@ describe.only('calc', () => { const result = calc(3).add(5); //Then expect(result.v).to.equal(8); - }); - }); + describe("minus", () => { it("should exist", () => { //Given @@ -64,9 +61,7 @@ describe.only('calc', () => { expect(result.v).to.equal(1); }); }); - - //.. - + describe("divide", () => { it("should exist", () => { //Given @@ -74,11 +69,8 @@ describe.only('calc', () => { //When //THen expect(c.divide).not.to.undefined; - }); - it("should be able to perform a valid division", () => { - //Given const c = calc(10); //When @@ -86,18 +78,15 @@ describe.only('calc', () => { //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"); - - + expect(() => c.divide(0)).to.throw("Division by 0 is not possible!"); }); }); + describe("sqrt", () => { it("should exist", () => { //Given @@ -105,7 +94,6 @@ describe.only('calc', () => { //When //THen expect(c.sqrt).not.to.undefined; - }); it("should be able to square a number", () => { //Given @@ -115,15 +103,15 @@ describe.only('calc', () => { //Then expect(result).to.equal(2); }); - it ("should handle negative numbers", () => { //Given const c = calc(-3); //When //Then - expect(() => c.sqrt()).to.throw("Square"); + expect(() => c.sqrt()).to.throw("Square root of negative value cannot be determined!"); }); }); + describe("times", () => { it("should exist", () => { //Given @@ -131,9 +119,7 @@ describe.only('calc', () => { //When //THen expect(c.times).not.to.undefined; - }); - it("should be able to multiply numbers", () => { //Given const c = calc(3); @@ -141,9 +127,7 @@ describe.only('calc', () => { const result = calc(3).times(10).v; //Then expect(result).to.equal(30); - }); - }); describe("modulo", () => { @@ -153,9 +137,7 @@ describe.only('calc', () => { //When //THen expect(c.modulo).not.to.undefined; - }); - it("should be able to perform modulo operation", () => { //Given const c = calc(10); @@ -164,7 +146,6 @@ describe.only('calc', () => { //Then expect(result).to.equal(0); }); - }); describe("multiply operations", () => { @@ -175,13 +156,6 @@ describe.only('calc', () => { const result = c.add(4).minus(3).times(6).v; //THen expect(result).to.equal(24); - }); - }); - - - - }); - From 37d12f89f8d73f751928b59986d2bc98a8a90203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Bakosi?= Date: Mon, 7 Dec 2020 08:45:26 +0100 Subject: [PATCH 4/4] Solutions of module-4 tasks --- module-4/arrayEqual.js | 23 ++++++++++++++++- module-4/arraySorted.js | 28 ++++++++++++++++++++- module-4/arraySum.js | 24 +++++++++++++++++- module-4/longestString.js | 23 ++++++++++++++++- module-4/romanToDec.js | 42 +++++++++++++++++++++++++++++++- module-4/test/romanToDec.json | 19 +++++++++++++++ module-4/test/romanToDec.spec.js | 20 +++++++++++++++ module-4/toCamelCase.js | 25 ++++++++++++++++++- 8 files changed, 198 insertions(+), 6 deletions(-) create mode 100644 module-4/test/romanToDec.json create mode 100644 module-4/test/romanToDec.spec.js diff --git a/module-4/arrayEqual.js b/module-4/arrayEqual.js index 09824ac..c02f229 100644 --- a/module-4/arrayEqual.js +++ b/module-4/arrayEqual.js @@ -10,4 +10,25 @@ * @param {Array} second The second array * @returns {boolean} true if the two arrays are equal, * false otherwise - */ \ No newline at end of file + */ + +function arrayEqual(first, second) { + const firstFlattend = first.flat(Infinity); + const secondFlattend = second.flat(Infinity); + const firstLength = firstFlattend.length; + const secondLength = secondFlattend.length; + + if (firstLength !== secondLength) { + console.warn(`The length of the arrays are different:\nLength of the first array: ${firstLength}. \nLength of the second array: ${secondLength}.`); + return false; + }; + for (let i = 0; i < firstLength; ++i) { + if (firstFlattend[i] !== secondFlattend[i]) { + console.warn(`Different element was found!:\nThe ${i}th element in the first array is: ${firstFlattend[i]}.\nThe ${i}th element in the second array is: ${secondFlattend[i]}.`); + return false; + } + } + return true; +}; + +module.exports = arrayEqual; diff --git a/module-4/arraySorted.js b/module-4/arraySorted.js index 7e4ebae..3d5e15f 100644 --- a/module-4/arraySorted.js +++ b/module-4/arraySorted.js @@ -11,4 +11,30 @@ * @param {string} ignore characters to ignore * @returns {boolean} true if the array is properly sorted, * false otherwise - */ \ No newline at end of file + */ + +function arraySorted(items, ignore) { + let itemsFlattend; + let replace = new RegExp(`[${ignore}]`, "g"); + + if (Array.isArray(items.flat(Infinity))) { + itemsFlattend = items.flat(Infinity) + } else { + itemsFlattend = items; + }; + for (let i = 0; i < itemsFlattend.length; ++i) { + itemsFlattend[i] = String(itemsFlattend[i]).toLowerCase().replace(/\s/g,""); + if (ignore !== undefined) { + itemsFlattend[i] = itemsFlattend[i].replace(replace, ""); + }; + }; + for (let i = 1; i < itemsFlattend.length; ++i) { + + if (itemsFlattend[i] < itemsFlattend[i - 1]) { + return false; + }; + }; + return true; +}; + +module.exports = arraySorted; diff --git a/module-4/arraySum.js b/module-4/arraySum.js index 9db243e..aa8a247 100644 --- a/module-4/arraySum.js +++ b/module-4/arraySum.js @@ -5,4 +5,26 @@ * * @param {Array} elements * @returns {number} summary of all integers or 0 in other cases - */ \ No newline at end of file + */ + +function arraySum(elements) { + let paramFlattend; + let sum = 0; + + if (typeof (elements) !== "object") { + return sum; + }; + if (Array.isArray(elements.flat(Infinity))) { + paramFlattend = elements.flat(Infinity) + } else { + paramFlattend = elements; + }; + paramFlattend.forEach(element => { + if (typeof (element) === "number") { + sum += element; + }; + }); + return sum; +}; + +module.exports = arraySum; diff --git a/module-4/longestString.js b/module-4/longestString.js index 731dd17..05408f9 100644 --- a/module-4/longestString.js +++ b/module-4/longestString.js @@ -5,4 +5,25 @@ * * @param {Array.} strings * @returns {string} longest string or empty string in other cases - */ \ No newline at end of file + */ + +function longestString(strings) { + let paramFlattend; + + if (typeof (strings) !== "object") { + return ""; + }; + if (Array.isArray(strings.flat(Infinity))) { + paramFlattend = strings.flat(Infinity) + } else { + paramFlattend = strings; + }; + paramFlattend = paramFlattend.filter((element) => { + return typeof (element) === "string"; + }); + return paramFlattend.sort((a, b) => { + return b.length - a.length; + }).sort()[0]; +}; + +module.exports = longestString; diff --git a/module-4/romanToDec.js b/module-4/romanToDec.js index e02d0d1..8501655 100644 --- a/module-4/romanToDec.js +++ b/module-4/romanToDec.js @@ -1,3 +1,13 @@ +const ROMAN_TABLE = { + "I": 1, + "V": 5, + "X": 10, + "L": 50, + "C": 100, + "M": 500, + "D": 1000, +} + /** * It recieves a Roman number (as string) * and converts it to it's Arabic (decimal) equivalent. @@ -6,4 +16,34 @@ * @param {string} roman * @returns {number} the Arabic (decimal) equivalent of the parameter * @throws Error in case of invalid input - */ \ No newline at end of file + */ + +function romanToDec(roman) { + let number = 0; + let next = 0; + let curr = 0; + + if (typeof roman !== "string") { + throw new Error("Invalid input datatype"); + }; + if (roman.length > roman.split("").filter(element => { return Object.keys(ROMAN_TABLE).includes(element)}).length) { + throw new Error("Invalid character in input"); + } + for (let i = 0; i < roman.length; i++) { + curr = ROMAN_TABLE[roman.charAt(i)]; + next = ROMAN_TABLE[roman.charAt(i + 1)]; + if (next === undefined) { + number += curr; + continue; + } + if(curr >= next) { + number +=curr; + continue; + } + number -= curr; + + }; + return number; +}; + +module.exports = romanToDec; diff --git a/module-4/test/romanToDec.json b/module-4/test/romanToDec.json new file mode 100644 index 0000000..e04819b --- /dev/null +++ b/module-4/test/romanToDec.json @@ -0,0 +1,19 @@ +[ + { + "arguments": ["CCC"], + "result": 300 + }, + { + "arguments": ["IV"], + "result": 4 + }, + { + "arguments": ["III"], + "result": 3 + }, + { + "arguments": ["MCDIX"], + "result": 1409 + } + +] \ No newline at end of file diff --git a/module-4/test/romanToDec.spec.js b/module-4/test/romanToDec.spec.js new file mode 100644 index 0000000..cecdbce --- /dev/null +++ b/module-4/test/romanToDec.spec.js @@ -0,0 +1,20 @@ +const romanToDec = require('../romanToDec'); +const expect = require('chai').expect; +const testData = require('./romanToDec.json'); + +describe.only('Module 4 - romanToDec', () => { + it('should be a function', () => { + expect(romanToDec).to.be.instanceOf(Function); + }); + it('should throw an error in case of invalid input type', () => { + expect(() => romanToDec(5)).to.throw("Invalid input datatype"); + }); + it('should throw an error in case of invalid character', () => { + expect(() => romanToDec("CXT")).to.throw("Invalid character in input"); + }); + testData.forEach(data => { + it('should return proper value for ' + JSON.stringify(data.arguments), () => { + expect(romanToDec.apply(null, data.arguments)).to.equal(data.result); + }); + }); +}); diff --git a/module-4/toCamelCase.js b/module-4/toCamelCase.js index 1a8ec37..eb12426 100644 --- a/module-4/toCamelCase.js +++ b/module-4/toCamelCase.js @@ -1,7 +1,30 @@ +const ALLOWED_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789"; + /** * It returns the camel-case version of string. * E.g.: simple lowercase string => SimpleLowercaseString * * @param {string} toConvert * @returns {string} camel-case string or empty string in other cases - */ \ No newline at end of file + */ + +function toCamelCase(toConvert) { + if (typeof (toConvert) !== "string") { + return ""; + } + return toConvert + .split(" ") + .map(word => word.split("").filter(c => { + return ALLOWED_CHARACTERS.includes(c.toLowerCase()); + }).join("")) + .filter(Boolean) + .map((word, i) => { + if (!i) { + return word.toLowerCase(); + } + return word[0].toUpperCase() + word.slice(1).toLowerCase(); + }) + .join(""); +}; + +module.exports = toCamelCase;