Skip to content

Added a solution for coding-Challenge-6. #27

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

Open
wants to merge 1 commit 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
74 changes: 74 additions & 0 deletions question1-solution.js
Original file line number Diff line number Diff line change
@@ -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));
21 changes: 21 additions & 0 deletions question2-solution.js
Original file line number Diff line number Diff line change
@@ -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);
72 changes: 72 additions & 0 deletions question3-solution.js
Original file line number Diff line number Diff line change
@@ -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