Skip to content

Instantly share code, notes, and snippets.

@hillal20
Last active January 13, 2019 23:56
Show Gist options
  • Save hillal20/697929c559cb435f4494e89f5ba13c98 to your computer and use it in GitHub Desktop.
Save hillal20/697929c559cb435f4494e89f5ba13c98 to your computer and use it in GitHub Desktop.
GleefulWorseArgument created by hillal20 - https://repl.it/@hillal20/GleefulWorseArgument
const arr = [6,2,4,10]
const bill = (arr)=>{
let newArr = [];
let diff = Math.pow(10,20)
for(let i = 0; i < arr.length; i ++){
for( let j = i+1; j < arr.length + 1 ; j ++){
if(Math.abs(arr[i] - arr[j]) <= diff ){
diff = Math.abs(arr[i] - arr [j]);
newArr.push({diff:diff, pair:[arr[i],arr[j]]})
}
}
}
//console.log(newArr)
let newArr2 = newArr.filter(e => { return e.diff === diff})
//console.log(newArr2);
let newArr3 = [];
newArr2.map(e =>{
newArr3.push(e.pair)
})
//console.log(newArr3)
newArr3.map( e =>{
if(e[0]>e[1]){
[e[0],e[1]] = [e[1],e[0]]
}
})
//console.log(newArr3);
newArr3.sort((a,b)=>{
return a[0] > b[0]
})
//console.log(newArr3)
newArr3.forEach(e =>{
console.log(`${e[0]} ${e[1]}\n`)
})
//return newArr3
}
bill(arr);
// let string = "alfo"
// let arr = string.split('');
// console.log(arr);
// const pro = (arr)=>{
// const result = [];
// let combination;
// const helper = (str,newArr)=>{
// for(let i = 0 ; i < newArr.length; i++){
// combination = str + newArr[i]
// result.push(combination)
// helper(combination, newArr.slice(i+1));
// }
// }
// helper('',arr);
// return result;
// }
// console.log(pro(arr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment