DEV Community

Discussion on: Can you solve this interview problem?

Collapse
 
d3skdev profile image
d3skdev
Collapse
 
0shuvo0 profile image
Shuvo

Seems like your code only works is it elements of array is in a particular order

I shuffled the array again and its not working

let url = `[ "6Ly9kZXYudG", "9jb21tZW5", "8vMHNodXZvMC", "aHR0cHM", "0LzFqZTFt" ]`;

const base64 = JSON.parse(url);
const sort = base64.sort(() => 0 - Math.random()).join("");
const decode = atob(sort);

document.getElementById("url").innerHTML =  decode;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
d3skdev profile image
d3skdev

i see, please check my update

Thread Thread
 
0shuvo0 profile image
Shuvo

It works
Pro tip: you can optimize it by first getting the permutation of the array

Collapse
 
jayakrishnancn profile image
jayakrishnancn

Quick question, why did you use sort(() => 0 - Math.random()) ?
to reverse sort?

Collapse
 
sarathm09 profile image
sarathm09

No, it shuffles the array elements. Basically he is shuffling the array again and again till he gets it right