-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge6_solutions.js
127 lines (101 loc) · 2.85 KB
/
challenge6_solutions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Task 1 solution beginning
function inner(array) {
//sort array
var arraySorted = array.sort(function(a, b){return a - b});
var breakValues = [0];
var result = [];
// Calculate break values
for (var i = 0; i < arraySorted.length; i++) {
if (arraySorted[i] < arraySorted[i + 1]) {
var count = i + 1;
breakValues = breakValues.concat(count);
}
}
// Fill in result array
for (var k = 0; k < breakValues.length; k++) {
//divide array at break values
result[k] = arraySorted.slice(breakValues[k], breakValues[k + 1]);
if (result[k].length == 1) {
//write elements to result array
result[k] = result[k][0];
}
}
return result
}
function answer(array) {
var numbers = [];
var strings = [];
var result = [];
//check if array has strings and numbers
array.map(function organizeArray(element) {
if (typeof element === "number") {
//build array with only numbers
numbers.push(element);
//run function for numbers
inner(numbers);
result[0] = numbers;
} else {
//build array with only strings
strings.push(element);
//run function for strings
inner(strings);
result[1] = strings;
}
})
return result
}
answer([1,'2',4,'591',392,'391',2,'5',10,'2',1,'1',1,'20',20]);
// Task 1 solution end
// Task 2 solution beginning
function answer(array, number) {
var array1 = array.map(function checkElements(element) {
//calculate the value so that the addition results in number
var secondElement = number - element;
//check if calculated value is in the array
if (secondElement != element && array.includes(secondElement)) {
//write the pair of numbers to result array
var result = [element, secondElement];
}
return result
})
//this part makes sure that function doesn/t return two the same numbers
if (array1.includes(undefined)) {
array1.sort(function(a, b){return a - b});
array1.pop();
}
//remove pairs of numbers that repeat
array1 = array1.slice(0, array1.length / 2);
return array1
}
answer([1,2,3,4,5,6,7,8,9], 10);
// Task 2 solution end
// Task 3 solution beginning
function rgb2hex(array) {
var result = "";
array.map(component => {
//changes rgb to hex component
var rgb = Number(component).toString(16);
//adds padding to components with length 1
rgb = rgb.length == 1 ? "0" + rgb : rgb;
result += rgb;
})
return '#' + result
}
function hex2rgb(value) {
//finds red, green and blue components and writes them to array
var result = [value.slice(0,2), value.slice(2,4), value.slice(4)];
//changes hex to rgb
result = result.map(component => parseInt(component, 16))
return 'RGB ' + result.toString();
}
//function that recognises hex and rgb format
function convertHexRgb(value) {
if (value.includes(",")) {
var array = value.split(",");
return rgb2hex(array);
} else {
return hex2rgb(value);
}
}
convertHexRgb('FF0000');
// Task 3 solution end