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

Create Coding Singh #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions Coding Singh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//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].

function cleanRoom (array){
let returnArray = [];
let subArray = [];
array.sort(function(a,b){return a-b});
// debugger;
subArray.push(array[0])
for (let i=1; i<array.length; i++){
if(array[i] === array [i-1]){
subArray.push(array[i]);
} else {
returnArray.push(subArray);
subArray=[];
subArray.push(array[i]);
}
};
return returnArray;
}


//Bonus: Make it so it organizes strings differently
// from number types. i.e. [1, "2", "3", 2] should return
// [[1,2], ["2", "3"]]
function cleanRoom2 (array){
let returnArray = [[],[]];
for (element of array){
typeof element === "number" ? returnArray[0].push(element) : returnArray[1].push(element);
};
returnArray[0].sort(function(a,b){return a-b;});
returnArray[1].sort(function(a,b){return a-b;});
console.log(array);
return returnArray;
}

// 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]

function bondnumbers(array,sum){
array.sort();
let returnArray = [];
for (element of array) {
if (typeof array.find((a)=>{return a === sum-element}) === "number"){
returnArray.push(element,sum-element);
break;
}
}
return returnArray;
}

// Question 3: Write a function that converts HEX to RGB. Then Make that function
// auto-dect the formats so that if you enter HEX color format it returns RGB and
// if you enter RGB color format it returns HEX.

function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}

function rgbHexToggle(input){
if (typeof input === "string"){
return "R: "+parseInt(input.slice(1,3),16)+", G: "+parseInt(input.slice(3,5),16)+", B: "+parseInt(input.slice(5,),16)
} else {
return "#" + componentToHex(input[0]) + componentToHex(input[1]) + componentToHex(input[2]);
}

}