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

Module-4 task solutions #61

Open
wants to merge 4 commits 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
20 changes: 18 additions & 2 deletions module-1/classification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;








module.exports = grade;
16 changes: 14 additions & 2 deletions module-1/euclidean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
module.exports = euclidean;

21 changes: 19 additions & 2 deletions module-1/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

module.exports = fibonacci;

142 changes: 141 additions & 1 deletion module-2/test/calc.spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { Then } = require('cucumber');
const calc = require('../calc');
const expect = require('chai').expect;

Expand All @@ -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);
});

});
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);
});
});
});
23 changes: 22 additions & 1 deletion module-4/arrayEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,25 @@
* @param {Array} second The second array
* @returns {boolean} true if the two arrays are equal,
* false otherwise
*/
*/

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;
28 changes: 27 additions & 1 deletion module-4/arraySorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,30 @@
* @param {string} ignore characters to ignore
* @returns {boolean} true if the array is properly sorted,
* false otherwise
*/
*/

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;
24 changes: 23 additions & 1 deletion module-4/arraySum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,26 @@
*
* @param {Array} elements
* @returns {number} summary of all integers or 0 in other cases
*/
*/

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;
23 changes: 22 additions & 1 deletion module-4/longestString.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,25 @@
*
* @param {Array.<string>} strings
* @returns {string} longest string or empty string in other cases
*/
*/

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;
Loading