From cfc75cb16ac6afcaeccd6e170e169389e634f298 Mon Sep 17 00:00:00 2001 From: Ilona Khain Date: Mon, 14 Jun 2021 23:15:57 +0300 Subject: [PATCH] add solution for module 4: js arrays & strings --- module-4/arrayEqual.js | 24 ++++++++++++++- module-4/arraySorted.js | 26 ++++++++++++++++- module-4/arraySum.js | 15 +++++++++- module-4/longestString.js | 17 ++++++++++- module-4/romanToDec.js | 32 +++++++++++++++++++- module-4/test/romanToDec.json | 50 ++++++++++++++++++++++++++++++++ module-4/test/romanToDec.spec.js | 28 ++++++++++++++++++ module-4/toCamelCase.js | 26 ++++++++++++++++- 8 files changed, 212 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..51a90cc 100644 --- a/module-4/arrayEqual.js +++ b/module-4/arrayEqual.js @@ -10,4 +10,26 @@ * @param {Array} second The second array * @returns {boolean} true if the two arrays are equal, * false otherwise - */ \ No newline at end of file + */ +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; \ No newline at end of file diff --git a/module-4/arraySorted.js b/module-4/arraySorted.js index 7e4ebae..d6aab24 100644 --- a/module-4/arraySorted.js +++ b/module-4/arraySorted.js @@ -11,4 +11,28 @@ * @param {string} ignore characters to ignore * @returns {boolean} true if the array is properly sorted, * false otherwise - */ \ No newline at end of file + */ +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; \ No newline at end of file diff --git a/module-4/arraySum.js b/module-4/arraySum.js index 9db243e..161c6e5 100644 --- a/module-4/arraySum.js +++ b/module-4/arraySum.js @@ -5,4 +5,17 @@ * * @param {Array} elements * @returns {number} summary of all integers or 0 in other cases - */ \ No newline at end of file + */ +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; \ No newline at end of file diff --git a/module-4/longestString.js b/module-4/longestString.js index 731dd17..7bd4209 100644 --- a/module-4/longestString.js +++ b/module-4/longestString.js @@ -5,4 +5,19 @@ * * @param {Array.} strings * @returns {string} longest string or empty string in other cases - */ \ No newline at end of file + */ +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; \ No newline at end of file diff --git a/module-4/romanToDec.js b/module-4/romanToDec.js index e02d0d1..4bb5149 100644 --- a/module-4/romanToDec.js +++ b/module-4/romanToDec.js @@ -6,4 +6,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 + */ +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; \ No newline at end of file diff --git a/module-4/test/romanToDec.json b/module-4/test/romanToDec.json new file mode 100644 index 0000000..e5489fb --- /dev/null +++ b/module-4/test/romanToDec.json @@ -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 + } +] \ 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..489250e --- /dev/null +++ b/module-4/test/romanToDec.spec.js @@ -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(); + }); +}); \ No newline at end of file diff --git a/module-4/toCamelCase.js b/module-4/toCamelCase.js index 1a8ec37..c467622 100644 --- a/module-4/toCamelCase.js +++ b/module-4/toCamelCase.js @@ -4,4 +4,28 @@ * * @param {string} toConvert * @returns {string} camel-case string or empty string in other cases - */ \ No newline at end of file + */ +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; \ No newline at end of file