<p>Comma Separated List: <input type="text" pattern="[^,]+((,[^,]+)+)?"></p>
<p><input type="button" value="reverse"></p>
<p><pre id="output"></pre></p>
document.querySelector('input[type="button"]').addEventListener('click', revClick);
function revClick()
{
let input = document.querySelector('input[type="text"]').value.split(',');
function indexToXY(index, width)
{
let x = index % width;
let y = Math.floor(index / width);
return [x,y];
}
function xyToIndex(x, y, width)
{
return (x + (y * width));
}
function reverse(list)
{
let width = Math.ceil(Math.sqrt(list.length));
let rev = list.reduce((oup, item, index) =>
{
let [x, y] = indexToXY(index, width);
let pole = Math.floor(width/2);
let polex = x - pole;
let poley = y - pole;
let r = Math.sqrt((polex*polex)+(poley*poley));
let fi = Math.atan2(poley, polex);
fi += Math.PI;
let newx = Math.round((Math.cos(fi)*r)+pole);
let newy = Math.round((Math.sin(fi)*r)+pole);
let newIndex = xyToIndex(newx, newy, width);
oup[newIndex] = item;
return oup;
}, []);
return rev;
}
document.getElementById('output').innerHTML = reverse(input).filter(x => x).join(',');
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.