A Pen by Erez Lieberman on CodePen.
Created
July 9, 2019 16:03
-
-
Save erezLieberman/54d8a3a469c2d9881d527c28ca70e6d9 to your computer and use it in GitHub Desktop.
pXqeNE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function areSimilar(a, b) { | |
const compare = (first,second) => first.toString() == second.toString(); | |
if([...a].sort((a,b) => a - b).toString() !== [...b].sort((a,b) => a - b).toString()){ | |
return false | |
} | |
if(!compare(a,b)){ | |
for(let i = 0; i<a.length; i++){ | |
console.log("before",b) | |
let placeholder = b[0] | |
b[0] = b[i]; | |
b[i] = placeholder; | |
// b[0] = placeholder; | |
console.log("after",b) | |
if(compare(a,b)){ | |
return true; | |
} | |
} | |
for(let i = 0; i<a.length; i++){ | |
console.log("before",b) | |
let placeholder = b[1] | |
b[1] = b[i]; | |
b[i] = placeholder; | |
// b[0] = placeholder; | |
console.log("after",b) | |
if(compare(a,b)){ | |
return true; | |
} | |
} | |
for(let i = 0; i<a.length; i++){ | |
console.log("before",b) | |
let placeholder = b[2] | |
b[2] = b[i]; | |
b[i] = placeholder; | |
// b[0] = placeholder; | |
console.log("after",b) | |
if(compare(a,b)){ | |
return true; | |
} | |
} | |
} | |
return compare(a,b) | |
} | |
const _a = [2, 3, 1]; | |
const _b = [1, 3, 2]; | |
console.log(areSimilar(_a, _b)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment