Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* The input message is given as a sequence of words separated by a space. The words are converted into
- a sequence of letters the following way: until all words disappear, the last letter of each word (if exists)
- is removed from the word and is appended to the output sequence of letters.
- The next step in the encryption algorithm is to move each letter (from positions 0, 1, …, n-1) on the right k times. The number k is taken from the number of the letter in the Latin alphabet regardless of its casing ('a' 1, 'b' 2, …, 'z' 26). When a letter is moved to the right, if it is the last letter of the sequence, its next position is the first position in the sequence, just before all the letters. */
- function solve(args) {
- args = args[0].split(/[^a-z]+/i).filter(Boolean);
- var maxWordLength = 1;
- args.forEach(function(word) {
- maxWordLength = (word.length > maxWordLength) ? word.length : maxWordLength;
- return maxWordLength;
- });
- var lettersSeqArr = [];
- for (var i = 0; i < maxWordLength; i += 1) {
- for (var word in args) {
- if (args.hasOwnProperty(word)) {
- var wordArr = (args[word]).split('').filter(Boolean);
- wordArr.reverse();
- if (wordArr[i]) {
- lettersSeqArr.push(wordArr[i]);
- }
- }
- }
- }
- Array.prototype.insertAt = function(index, element1) {
- if (index >= 0) {
- this.splice(index, 0, element1);
- }
- };
- Array.prototype.removeFrom = function(index, countElemToRemove) {
- if (index >= 0 && index < this.length) {
- this.splice(index, countElemToRemove);
- }
- };
- for (i = 0; i < lettersSeqArr.length; i += 1) {
- var currentLetter = lettersSeqArr[i];
- var movingPositionsLetter = currentLetter.toLowerCase().charCodeAt(0) - 96;
- movingPositionsLetter = (movingPositionsLetter + i) % lettersSeqArr.length;
- lettersSeqArr.removeFrom(i, 1);
- lettersSeqArr.insertAt(movingPositionsLetter, currentLetter);
- }
- console.log(lettersSeqArr.join(''));
- }
- solve(['Fun exam right']);
- solve(['Hi exam']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement