diff --git a/question1-solution.js b/question1-solution.js new file mode 100644 index 0000000..6f2eed0 --- /dev/null +++ b/question1-solution.js @@ -0,0 +1,74 @@ +/* Question 1: +Clean the room function: given an input of [1,2,4,591,392,391,2,5,10,2,1,1,1,20,20], +make a function that organizes these into individual array that is ordered. +For example answer(ArrayFromAbove) should return: [[1,1,1,1],[2,2,2], 4,5,10,[20,20], 391, 392,591]. +Bonus: Make it so it organizes strings differently from number types. i.e. [1, "2", "3", 2] should return [[1,2], ["2", "3"]] */ + +const input = [ + 4, + 1, + 2, + 4, + 591, + 392, + 391, + '2', + 5, + 10, + '23', + '1', + '1', + '4', + '4', + '4', + 2, + 1, + 1, + 1, + 4, + 20, + 20, +]; + +function getArrayByType(inputArray, type) { + return inputArray.filter((item) => typeof item === type); +} + +function getMap(array) { + const itemsMap = new Map(); + + array.forEach((item) => { + const mapItem = itemsMap.get(item); + + if (mapItem) { + itemsMap.set(item, [...mapItem, item]); + } else { + itemsMap.set(item, [item]); + } + }); + + return itemsMap; +} + +function organize(array) { + const result = []; + const mappedItems = getMap(array); + + mappedItems.forEach((item) => { + result.push(item.length === 1 ? item[0] : item); + }); + + return result; +} + +function cleanTheRoom(array) { + const strings = getArrayByType(array, 'string'); + const numbers = getArrayByType(array, 'number'); + + const numbersResult = organize(numbers); + const stringsResult = organize(strings); + + return stringsResult.concat(numbersResult); +} + +console.log(cleanTheRoom(input)); diff --git a/question2-solution.js b/question2-solution.js new file mode 100644 index 0000000..aeee94f --- /dev/null +++ b/question2-solution.js @@ -0,0 +1,21 @@ +/*Question 2: +Write a javascript function that takes an array of numbers and a target number. +The function should find two different numbers in the array that, when added together, +give the target number. For example: answer([1,2,3], 4)should return [1,3] + */ + +const inputArray = [1, 2, 1, 1, 1, 1, 13]; +const targetNumber = 14; + +function findPairs(input, target) { + for (const element of input) { + const pair = input.find((item) => item + element === target); + if (pair) { + return [element, pair]; + } + } + + return 'error. there is no pairs with given target value.'; +} + +findPairs(inputArray, targetNumber); diff --git a/question3-solution.js b/question3-solution.js new file mode 100644 index 0000000..1a8c31e --- /dev/null +++ b/question3-solution.js @@ -0,0 +1,72 @@ +/* +Question 3: +Write a function that converts HEX to RGB. +Then Make that function autodect the formats so that if you enter HEX color format it returns RGB and if you enter RGB color format it returns HEX. +Bonus: Release this tool as a npm package.*/ + +const HEX = 'HEX'; +const RGB = 'RGB'; + +function determineColorType(color) { + return /^#[0-9A-F]{6}[0-9a-f]{0,2}$/i.test(color) ? HEX : RGB; +} + +function stringToHex(component) { + var hex = component.toString(16); + return hex.length == 1 ? '0' + hex : hex; +} + +function convertHexToRgb(hex) { + var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); + return result + ? [ + 'rgb(', + parseInt(result[1], 16), + ',', + parseInt(result[2], 16), + ',', + parseInt(result[3], 16), + ')', + ].join('') + : null; +} + +function stringToRGB(stringColor) { + var matches = /rgb\((\d+),(\d+),(\d+)\)/.exec(stringColor); + return matches + ? { + r: parseInt(matches[1]), + g: parseInt(matches[2]), + b: parseInt(matches[3]), + } + : null; +} + +function convertRgbToHex(input) { + const rgb = stringToRGB(input); + return '#' + stringToHex(rgb?.r) + stringToHex(rgb?.g) + stringToHex(rgb?.b); +} + +function convertColor(input) { + const colorType = determineColorType(input); + switch (colorType) { + case HEX: + return convertHexToRgb(input); + case RGB: + return convertRgbToHex(input); + default: + 'error. The requested color type is not supported by this converter.'; + break; + } +} + +console.log('Test data:', '#D46A6A', '#801515', '#D66A6A'); +console.log(convertColor('#D46A6A')); +console.log(convertColor('#801515')); +console.log(convertColor('#D66A6A')); +console.log('---------------------------------'); +console.log(convertColor(convertColor('#D46A6A'))); +console.log(convertColor(convertColor('#801515'))); +console.log(convertColor(convertColor('#D66A6A'))); + +// NPM package link: https://github.com/IlonaZaika/my-color-converter