Отображение символов в алфавитные индексы
[].map.call ("fruit", c => c.charCodeAt (0)^96)
//[6, 18, 21, 9, 20]
Чтобы переместить ваше сообщение с заданным ключом
function trnsps (key, msg) {
let {map}=[],_l,_n=0;
//Build the lookup indices from the key
let _key = key
.split('') //Split the key
.sort() //Sort it alphabetically
.map ((l,i) => key.indexOf (l)) //map it to the original index
.map (n => {
let r=_l==n?n+ ++_n:n+(_n=0);
_l=n;
return r
}) //increase the index for every consecutive character
//Map the alphabetical indices to characters
msg = map.call (msg, c => typeof c=="number"?String.fromCharCode(c^96):c)
//Encode the input with the given key
let enc = map.call (msg, (c,i) => (
msg[_key[i]]
));
//Map the message to column indexes
return msg.map (c => -~enc.indexOf (c));
}
trnsps("fruit", "fruit");
trnsps ("fruit", [6,18,21,9,20])
trnsps ("frooot", "fruit")
console.log (
trnsps ("froot", [6,18,21,9,20])
)
<script>
function trnsps (key, msg) {
let {map}=[],_l,_n=0;
//Build the lookup indices from the key
let _key = key
.split('') //Split the key
.sort() //Sort it alphabetically
.map ((l,i) => key.indexOf (l)) //map it to the original index
.map (n => {
let r=_l==n?n+ ++_n:n+(_n=0);
_l=n;
return r
}) //increase the index for every consecutive character
//Map the alphabetical indices to characters
msg = map.call (msg, c => typeof c=="number"?String.fromCharCode(c^96):c)
//Encode the input with the given key
let enc = map.call (msg, (c,i) => (
msg[_key[i]]
));
//Map the message to column indexes
return msg.map (c => -~enc.indexOf (c));
}
</script>