Skip to content
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

My solution #26

Open
wants to merge 2 commits 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
56 changes: 56 additions & 0 deletions solution_1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const arrToSort=[1,2,4,591,392,391,2,5,10,2,1,1,1,20,20];

const cleanTheRoom = (arr)=>{
let sorted = arr.toSorted((a,b)=> a-b);
for(let i =0 ;i<sorted.length;i++){
if(typeof(sorted[i])==='string'){
grupByType(sorted);
break;
} else
grupIdentical(sorted);
}
}

const grupIdentical=(arr)=>{
let grup=[];
let count=1;
for(let i = 0;i<arr.length;i++){
for(let j = i+1;j<arr.length;j++){
if(arr[i]===arr[j]){
count++
}
}
if(count>1&& i===0){
i+=count;
grup.push([arr.toSpliced(i,arr.length)]);
i--;
count = 1;
} else if(count>1&& i>3){
i+=count;
let subArr=arr.toSpliced(i,arr.length).toReversed();
grup.push([subArr.toSpliced(count,subArr.length)]);
subArr=[];
i--;
count = 1;
}
else{
grup.push(arr[i]);
}
}
console.log(grup);
}

const grupByType = (arr)=>{
let mainArr=[];
let stringArr=[];
let numArr=[];
for(let i =0 ;i<arr.length;i++){
if(typeof(arr[i])==='string'){
stringArr.push(arr[i]);
}else{
numArr.push(arr[i]);
}
}
mainArr.push([numArr],[stringArr]);
console.log(mainArr);
}
11 changes: 11 additions & 0 deletions solution_2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const toSume = (nums,target)=>{
let sum=0;
let newArray=[];
for(let i=0;i<nums.length;i++)
for(let j=1;j<nums.length;j++){
sum=nums[i]+nums[j];
if(sum==target && i!=j){
return newArray=[i,j];
}
}
}
33 changes: 33 additions & 0 deletions solution_3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const 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
const convertHexRgb = (value)=> {
if (value.includes(",")) {
var array = value.split(",");
return rgb2hex(array);

} else {
return hex2rgb(value);

}
}

convertHexRgb('FF0000');