Skip to content
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
24 changes: 23 additions & 1 deletion module-4/arrayEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,26 @@
* @param {Array} second The second array
* @returns {boolean} true if the two arrays are equal,
* false otherwise
*/
*/
function isTypeOfElementsIdentical(array1, array2) {
return array1.every((index) => typeof (array1[index]) === typeof (array2[index]));
}

function findFirstDifferentElementOfArray(array1, array2) {
return array1.filter(e => !array2.includes(e));
}

function arrayEqual(array1, array2) {
if (array1.length !== array2.length) {
console.warn(`${array1.length} vs ${array2.length}`);
return false;
}
else if (array1 == array2.toString() && isTypeOfElementsIdentical(array1, array2) === true) {
return true;
}
else {
console.warn(`${findFirstDifferentElementOfArray(array1, array2)} vs ${findFirstDifferentElementOfArray(array2, array1)}`);
return false;
}
}
module.exports = arrayEqual;
26 changes: 25 additions & 1 deletion module-4/arraySorted.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,28 @@
* @param {string} ignore characters to ignore
* @returns {boolean} true if the array is properly sorted,
* false otherwise
*/
*/
const arrayEqual = require('./arrayEqual');
function checkTypeOfArrayElements(array, type) {
return array.every(element => typeof (element) === type);
}
function arraySorted(items) {
let sortedArray = items.slice();
sortedArray.sort(function (a, b) {
if (checkTypeOfArrayElements(sortedArray, "number")) {
return sortedArray.sort();
}
else if (checkTypeOfArrayElements(sortedArray, "string")) {
return a.localeCompare(b, 'en', { ignorePunctuation: true, sensitivity: 'base' });
}
else { throw new Error("algorithm is undefined"); }
});;
if (arrayEqual(items, sortedArray)) {
return true;
}
else {
return false;
}
}

module.exports = arraySorted;
15 changes: 14 additions & 1 deletion module-4/arraySum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,17 @@
*
* @param {Array} elements
* @returns {number} summary of all integers or 0 in other cases
*/
*/
function arraySum(array) {
let sum = 0;
if (Array.isArray(array)) {
let elements = array.flat(Infinity);
for (let element of elements) {
if (typeof (element) === "number") {
sum += element;
}
}
}
return sum;
}
module.exports = arraySum;
17 changes: 16 additions & 1 deletion module-4/longestString.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@
*
* @param {Array.<string>} strings
* @returns {string} longest string or empty string in other cases
*/
*/
function longestString(arrayOfString) {
let maxLength = 0;
let maxLengthElement = '';
if (Array.isArray(arrayOfString)) {
arrayOfString.sort();
for (let element of arrayOfString) {
if (element.length > maxLength) {
maxLength = element.length;
maxLengthElement = element;
}
}
}
return maxLengthElement;
}
module.exports = longestString;
32 changes: 31 additions & 1 deletion module-4/romanToDec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,34 @@
* @param {string} roman
* @returns {number} the Arabic (decimal) equivalent of the parameter
* @throws Error in case of invalid input
*/
*/
let rules = {
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000
};
function getArabicValue(symbolsOfRoman, index) {
let valueRoman = symbolsOfRoman[index];
if (rules[valueRoman] !== undefined) {
return rules[valueRoman];
}
else throw new Error(`Invalid symbol in input ${valueRoman}`);
}
function romanToDec(roman) {
if (typeof (roman) === "string") {
let symbolsOfRoman = roman.split("");
let result = 0;
for (let index = 0; index < symbolsOfRoman.length - 1; index++) {
let valueArabic = getArabicValue(symbolsOfRoman, index);
let valueArabicNext = getArabicValue(symbolsOfRoman, index + 1);
result = (valueArabic < valueArabicNext) ? result -= valueArabic : result += valueArabic;
}
return result + rules[symbolsOfRoman[symbolsOfRoman.length - 1]];
}
else throw new Error(`Invalid input ${roman}`);
}
module.exports = romanToDec;
50 changes: 50 additions & 0 deletions module-4/test/romanToDec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"arguments": [ "I" ],
"result": 1
},
{
"arguments": [ "IV" ],
"result": 4
},
{
"arguments": [ "V" ],
"result": 5
},
{
"arguments": [ "IX" ],
"result": 9
},
{
"arguments": [ "X" ],
"result": 10
},
{
"arguments": [ "XL" ],
"result": 40
},
{
"arguments": [ "CCC" ],
"result": 300
},
{
"arguments": [ "CD" ],
"result": 400
},
{
"arguments": [ "D" ],
"result": 500
},
{
"arguments": [ "CM" ],
"result": 900
},
{
"arguments": [ "M" ],
"result": 1000
},
{
"arguments": [ "MCMIV" ],
"result": 1904
}
]
28 changes: 28 additions & 0 deletions module-4/test/romanToDec.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const romanToDec = require('../romanToDec');
const expect = require('chai').expect;
const testData = require('./romanToDec.json');

describe('Module 4 - romanToDec', () => {
it('should be a function', () => {
expect(romanToDec).to.be.instanceOf(Function);
});

testData.forEach(data => {
it('should return proper value for ' + JSON.stringify(data.arguments), () => {
expect(romanToDec(...data.arguments)).to.equal(data.result);
});
});

it('should throw an error when incorrect type of input ' + 'integer', () => {
return expect(romanToDec.bind(null, 0)).to.throw();
});
it('should throw an error when incorrect type of input ' + 'boolean', () => {
return expect(romanToDec.bind(null, true)).to.throw();
});
it('should throw an error when incorrect type of input ' + 'object', () => {
return expect(romanToDec.bind(null, [])).to.throw();
});
it('should throw an error when input contains not Roman symbol ', () => {
return expect(romanToDec.bind(null, "ABC")).to.throw();
});
});
26 changes: 25 additions & 1 deletion module-4/toCamelCase.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,28 @@
*
* @param {string} toConvert
* @returns {string} camel-case string or empty string in other cases
*/
*/
const ALLOWED_CHARACTERS = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
function toCamelCase(toConvert) {
if (typeof (toConvert) === "string") {
let words = toConvert.split(/\s+/).filter(word => word !== '');
for (let i = 0; i < words.length; i++) {
let wordSymbols = words[i].split("").filter((wordSymbol) => ALLOWED_CHARACTERS.includes(wordSymbol));
if (i === 0 && ALLOWED_CHARACTERS.includes(wordSymbols[0])) {
wordSymbols[0] = wordSymbols[0].toLowerCase();
}
else {
if (ALLOWED_CHARACTERS.includes(wordSymbols[0])) {
wordSymbols[0] = wordSymbols[0].toUpperCase();
}
}
words[i] = wordSymbols.join("");
}
return words.join("");
}
else {
return '';
}
}

module.exports = toCamelCase;