I am working on a Url decoding and encoding system. But for some odd reason, it will only decode/encode certain string. Also, it seems to decode/encode a string when it has a certain piece in it. It is quite hard to explain but it is quite confusion as to what this might not work. I tried figuring out the problem of it but it just makes the whole issue seem more illogical than logical.
I hope someone can help me with this. With explanation to it. I would like it if the code is in the same style as it is as well.
I also know that there are probably some packages online to easily do that but I would rather just make my own. It's a way I can practice JS more.
// I know I don't have all the characters markek. I am doing that later
var Url = {
filterEncode : ["%2B","%3A","%3D","%3F","%2F","%26","%252F","%253A","%253D","%253F","%252B"],
filterDecode : ["+",":","=","?","/","&","%2F","%3A","%3D","%3F","%2B"],
decode : function(decodeText){
let returnString, a, b;
let filterEncode = Url.filterEncode;
let filterDecode = Url.filterDecode;
for (a = 0; a < filterEncode.length; a++){
let regexEn = new RegExp(filterEncode[a],"g");
let regexDe = new RegExp("/" + filterDecode[a],"g");
let regex = new RegExp(regexEn,"g");
let array = (decodeText.match(regex) || []).length
for (b = 0; b < array; b++){
returnString = decodeText.replace(filterEncode[a],filterDecode[a]);
decodeText = returnString;
}
}
return returnString;
},
encode : function(encodeText){
let returnString, a, b;
let filterEncode = Url.filterEncode;
let filterDecode = Url.filterDecode;
for (a = 0; a < filterEncode.length; a++){
let regexEn = new RegExp("[" + filterEncode[2] + "]","g");
let regexDe = new RegExp("[" + filterDecode[2] + "]","g");
let regex = new RegExp(regexEn,"g");
let array = (encodeText.match(regex) || []).length;
for (b = 0; b < array; b++){
returnString = encodeText.replace(filterDecode[a],filterEncode[a]);
encodeText = returnString;
}
}
return returnString;
}
}
// Saying it is undefined
console.log(Url.encode("="));
// Encodes it just find
console.log(Url.encode("%3F"));
// Encodes both of them but for some odd reason encodes the
// equal sign twice.
console.log(Url.encode("%3F ="));
I do hope everything seems clear as to what my problem is about. I usually would just try searching on here for an answer, but this problem is so confusion, I don't know what I exactly should search for.
Thanks!
Some of your strings in filterDecode have special meaning as regular expressions. When converting the string to a regular expression, you need to wrap each character in [] so it will be matched literally.
You don't need to concatenate / when creating regexDe.
There's no need for the for(b...) loops. Use the regular expression in the replace() call and it will perform all the replacements at once, since it has the g flag.
Put the encode strings that contain % at the beginning of the array. Otherwise, when you encode something like = as %3D, a later iteration of the outer loop will re-encode that as %253D. You only want to encode this if it was in the original string, not an intermediate step.
var Url = {
filterDecode: ["%252F", "%253A", "%253D", "%253F", "%252B", "%2B", "%3A", "%3D", "%3F", "%2F", "%26"],
filterEncode: ["%2F", "%3A", "%3D", "%3F", "%2B", "+", ":", "=", "?", "/", "&"],
strToRe: function(str) {
let reStr = str.split("").map(c => '[' + c + ']').join('');
return new RegExp(reStr, "g");
},
decode: function(decodeText) {
let a;
let filterEncode = Url.filterEncode;
let filterDecode = Url.filterDecode;
for (a = 0; a < filterDecode.length; a++) {
decodeText = decodeText.replace(Url.strToRe(filterDecode[a]), filterEncode[a]);
}
return decodeText;
},
encode: function(encodeText) {
let a, b;
let filterEncode = Url.filterEncode;
let filterDecode = Url.filterDecode;
for (a = 0; a < filterEncode.length; a++) {
encodeText = encodeText.replace(Url.strToRe(filterEncode[a]), filterDecode[a]);
}
return encodeText;
}
}
console.log(Url.encode("="));
console.log(Url.decode("%3D"));
console.log(Url.encode("%3F"));
console.log(Url.decode("%253F"));
console.log(Url.encode("%3F ="));
console.log(Url.decode("%253F %3D"));
Related
This is my first post so I hope im doing this correctly.
I am taking a coding class and we were asked to make a piece of code that will ask for the input of a phrase, and will return in the console that phrase with the capital letters moved to the front, but still in the same order. Then print to the console this reordered phrase. (We aren't allowed to use arrays)
For example:
Inputting "HeLLoTherE" would return "HLLTEeoher"
However the problem is im having issues understanding how to write this code. How can I make the code select these capital letters and move them to the front? using .toUpperCase()? How can i make that select the letter and move it in front of the rest?
If someone could show me an example of how this is done and explain it a little i would greatly appreciate it :)
You might just start with a the most straight forward algorithm to get something working.
let value = "HeLLoTherE";
let result = "";
for (let char of value) {
if (char >= "A" && char <= "Z") {
result += char;
}
}
for (let char of value) {
if (char >= "a" && char <= "z") {
result += char;
}
}
console.log(result);
You could then consolidate the 2 loops by combining the conditions.
let value = "HeLLoTherE";
let upper = "";
let lower = "";
for (let char of value) {
if (char >= "A" && char <= "Z") {
upper += char;
} else if (char >= "a" && char <= "z") {
lower += char;
}
}
console.log(upper + lower);
Another way of solving this would be to use regex.
var value = "HeLLoTherE";
var upper = value.replace(/[^A-Z]*/g, "");
var lower = value.replace(/[^a-z]*/g, "");
console.log(upper + lower);
Well, you are not able to use arrays, which makes it a little bit difficult, however you can still do sommething.
Although I'm using a for loop, I'm not actually using arrays. Since strings allows the [] operator, you can use an index to select each character of the string and check if it's lowercase or uppercase.
In addition, you said you need to mantain the order of uppercase letters, so you couldn't just do newStr = upper + newStr, because it would revert the original order. So, I used the string.prototype.substring() to insert the uppercase character where it should be.
const str = "HeLLoTherE";
const moveUpperToFront = (target) => {
// Strings are immutable in js, so you cannot move one character
// to the front without using a new string.
let newStr = "";
// Number of uppercase letters that appeared.
// It's necessary because you need to mantain the original order
let upperNumber = 0;
// Iterate each character from beginning
for (let i = 0; i < str.length; ++i) {
// Is there an uppercase letter?
if (str[i].charCodeAt() >= 65 && str[i].charCodeAt() <= 90) {
newStr =
newStr.substring(0, upperNumber) +
str[i] +
newStr.substring(upperNumber, newStr.length);
++upperNumber;
}
// No uppercase letter?
else
newStr += str[i];
}
return newStr;
};
console.log(moveUpperToFront(str));
Following a solution which uses a for...of loop to iterate the input. It splits the input into capital and lowercase literals and then merges back together:
const exampleLiteral = 'HeLLoTherE';
const isUppercase = (literal) => literal === literal.toUpperCase() && literal !== literal.toLowerCase();
const prefixCapitalLetters = (literal) => {
let capitalLetters = '';
let lowerLetters = '';
for (let letter of literal) {
if(isUppercase(letter)) {
capitalLetters = capitalLetters.concat(letter);
continue;
}
lowerLetters = lowerLetters.concat(letter);
};
return capitalLetters+lowerLetters;
}
console.log(prefixCapitalLetters(exampleLiteral));
This is really not a very hard problem:
function rearrange(str) {
let result = "";
for (let c of str)
if (c >= 'A' && c <= 'Z')
result += c;
for (let c of str)
if (c < 'A' || c > 'Z')
result += c;
return result;
}
console.log(rearrange("Hello World, It Is A Beautiful Morning!"));
Find the upper-case characters, and add them to a result string. Then go back and find the other characters, and add them at the end. By looping through without any sorting, just simple iteration from start to finish, the order is preserved (other than the upper-case stuff).
The truly hard part of this would be coming up with a way to detect "upper-case" letters across all of Unicode. Some languages (well, orthographies) don't have the concept at all. JavaScript has ways that are more and less convenient to deal with that, but I suspect for the classroom material the OP has available so far, given the nature of the original question, such regex trickery would probably be inappropriate for an answer.
This answer tries to achieve the desired objective without using "arrays". It does use back-ticks, but that can be replaced with a simple string-concatenation if required.
Code Snippet
// move upper-case letters while
// keeping relative order same
const capsWithOrder = str => {
// initialize result variables
let capsOnly = "", restAll = "";
// iterate over the given string input
for (let i = 0; i < str.length; i++) {
// if character at index "i" is upper-case
// then, concatenate character to "capsOnly"
// else, concatenate to "restAll"
if (str[i] === str[i].toUpperCase()) capsOnly += str[i];
else restAll += str[i];
};
// after iterating over all characters in string-input
// return capsOnly concatenated with restAll
return `${capsOnly}${restAll}`;
};
console.log(capsWithOrder("HeLLoTherE"));
Explanation
Inline comments added in the snippet above.
Something like this
const string1 = 'HeLLoTherE'
const transform = string => {
const lower = string.split('').filter(c => c.charCodeAt() > 'a'.charCodeAt())
const upper = string.split('').filter(c => c.charCodeAt() < 'Z'.charCodeAt())
return [...upper, ...lower].join('')
}
console.log(transform(string1))
I think that must be work.
const sort = [
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split(''),
'abcdefghijklmnopqrstuvwxyz'.split('')
]
function listByValue(string) {
string = [...string];
let ret = [];
for (let i in sort)
ret = [...ret,...string.filter(e=>sort[i].includes(e))];
return ret.join('')
}
(Javascript)
functionName(“2 plus 3 is = 5!”);
would produce following message in console:
235 plus is =
I am unable to bring the numbers to the front, I am a little stumped
function frontNum(str) {
let emptyArr = [];
let rev = str.split("");
let expression = /[\d]/g;
for(let i = 0; i < rev.length; i++) {
if (rev[i].match(expression)) {
emptyArr += rev.pop(rev[i]);
}
}
console.log(emptyArr + rev.join(''));
}
frontNum("2 plus 3 is = 5!");
Since this is your home work I won't give you the correct version, but give you some pointers instead:
your emptyArr is an array, but you are adding data to it as if it was a string.
take a look at this topic, your pop causing problems
you can use your expression to capture all the digits and remove them from the string without need converting the string into array and loop through it (it can be done with 2 lines of code)
a way todo that
'use strict'
function frontNum(str)
{
let s = ''
, n = ''
for (let x of str)
{
if (/[0-9]/.test(x)) n += x
else s += x
}
console.log( n + s.replace(/ +/g,' ') )
}
frontNum('2 plus 3 is = 5!')
I've been trying to split a string by numbers. Basically I have a string such as:
"10x + 10"
JS splits it and gets:
["+10x", "+10"]
I've tried doing this a few times but haven't hit success. If I understand it right I just need to split it by + - / *. My best attempt was this:
var statement = ["10x + 10"]
var spliters = ['+', '-', '*', '/'];
for (i = 0; i < spliters.length; i++) {
for (o = 0; o < statement.length; o++) {
statement[o] = statment[o].split(spliters[i]);
for (p = 0; p < statment[o].length - 1; p++) {
statement[o][p] = spliters[i] + statement[o][p];
}
statement = flatten(statement);
}
}
But it doesn't work. Also I only want + and - to be in front on the array elements and not * and /. If anyone could help me with this is would be much appreciated.
Keep in mind that Array.prototype.split() can take a regular expressions:
let str = "10x + 10";
let arr = str.split(/[+|-|\*|/]/);
console.log(arr); // output: ["10x ", " 10"];
This is not a good way to parse arithmetical expressions, though. To do that, I recommend the Shunting-yard algorithm.
So I was doing some code challenge over on CoderByte and I can't get the Letter Exchange to work. The idea is to exchange all characters in a string with ones that are after them in alphabet. I tried with this code:
function LetterChanges(str) {
var string = "";
var i = 1;
var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var c = "";
for(i; i<=str.length; i++){
c = alp.charAt(alp.indexOf(str.charAt(i)));
string = string + c;
}
return string;
}
LetterChanges(readline());
But it won't work and I'm not sure why. It would be very helpful if you could point out my mistakes. :)
I know this is easy problem for lot of you guys out there, but I'm new to JavaScript.
Thanks.
For a start, your for loop needs some work. You should initialize i to 0, the first index, since accessing letters from a string is zero-based. The length of a string, however, is one-based, so if you were to try to access the character at str.length it will return null.
Try this:
function LetterChanges(str) {
var string = "";
var i = 0;
var upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var lower = "abcdefghijklmnopqrstuvwxyz";
var c = "";
for(i; i<str.length; i++){
if(str.charCodeAt(i) == str[i].toUpperCase().charCodeAt(0)){ // check if it's uppercase
c = upper[(upper.indexOf(str[i])+1)%upper.length]; // modulo for edge case if it's a zed
} else { // otherwise it's lowercase
c = lower[(lower.indexOf(str[i])+1)%lower.length]; // same thing as what's in the uppercase
}
string = string + c;
}
return string;
}
LetterChanges(readline());
Notice what I did, separating the upper and lower case letters. That's because I was assuming that you had to change the letter within the case. (so Z would become A, z would become a)
Say you have the following string:
FJKAUNOJDCUTCRHBYDLXKEODVBWTYPTSHASQQFCPRMLDXIJMYPVOHBDUGSMBLMVUMMZYHULSUIZIMZTICQORLNTOVKVAMQTKHVRIFMNTSLYGHEHFAHWWATLYAPEXTHEPKJUGDVWUDDPRQLUZMSZOJPSIKAIHLTONYXAULECXXKWFQOIKELWOHRVRUCXIAASKHMWTMAJEWGEESLWRTQKVHRRCDYXNT
LDSUPXMQTQDFAQAPYBGXPOLOCLFQNGNKPKOBHZWHRXAWAWJKMTJSLDLNHMUGVVOPSAMRUJEYUOBPFNEHPZZCLPNZKWMTCXERPZRFKSXVEZTYCXFRHRGEITWHRRYPWSVAYBUHCERJXDCYAVICPTNBGIODLYLMEYLISEYNXNMCDPJJRCTLYNFMJZQNCLAGHUDVLYIGASGXSZYPZKLAWQUDVNTWGFFY
FFSMQWUNUPZRJMTHACFELGHDZEJWFDWVPYOZEVEJKQWHQAHOCIYWGVLPSHFESCGEUCJGYLGDWPIWIDWZZXRUFXERABQJOXZALQOCSAYBRHXQQGUDADYSORTYZQPWGMBLNAQOFODSNXSZFURUNPMZGHTAJUJROIGMRKIZHSFUSKIZJJTLGOEEPBMIXISDHOAIFNFEKKSLEXSJLSGLCYYFEQBKIZZTQQ
XBQZAPXAAIFQEIXELQEZGFEPCKFPGXULLAHXTSRXDEMKFKABUTAABSLNQBNMXNEPODPGAORYJXCHCGKECLJVRBPRLHORREEIZOBSHDSCETTTNFTSMQPQIJBLKNZDMXOTRBNMTKHHCZQQMSLOAXJQKRHDGZVGITHYGVDXRTVBJEAHYBYRYKJAVXPOKHFFMEPHAGFOOPFNKQAUGYLVPWUJUPCUGGIXGR
AMELUTEPYILBIUOCKKUUBJROQFTXMZRLXBAMHSDTEKRRIKZUFNLGTQAEUINMBPYTWXULQNIIRXHHGQDPENXAJNWXULFBNKBRINUMTRBFWBYVNKNKDFR
I'm trying to find the smallest substring containing the letters ABCDA.
I tried a regex approach.
console.log(str.match(/[A].*?[B].*?[C].*?[D].*?[A]/gm).sort((a, b) => a.length - b.length)[0]);
This works, but it only find strings where ABCDA appear (in that order). Meaning it won't find substring where the letters appear in a order like this: BCDAA
I'm trying to change my regex to account for this. How would I do that without using | and type out all the different cases?
You can't.
Let's consider a special case: Assume the letters you are looking for are A, A, and B. At some point in your regexp there will certainly be a B. However, the parts to the left and to the right of the B are independent of each other, so you cannot refer from one to the other. How many As are matched in the subexpression to the right of the B depends on the number of As being already matched in the left part. This is not possible with regular expressions, so you will have to unfold all the different orders, which can be many!
Another popular example that illustrates the problem is to match opening brackets with closing brackets. It's not possible to write a regular expression asserting that in a given string a sequence of opening brackets is followed by a sequence of closing brackets of the same length. The reason for this is that to count the brackets you would need a stack machine in contrast to a finite state machine but regular expressions are limited to patterns that can be matched using FSMs.
This algorithm doesn't use a regex, but found both solutions as well.
var haystack = 'FJKAUNOJDCUTCRHBYDLXKEODVBWTYPTSHASQQFCPRMLDXIJMYPVOHBDUGSMBLMVUMMZYHULSUIZIMZTICQORLNTOVKVAMQTKHVRIFMNTSLYGHEHFAHWWATLYAPEXTHEPKJUGDVWUDDPRQLUZMSZOJPSIKAIHLTONYXAULECXXKWFQOIKELWOHRVRUCXIAASKHMWTMAJEWGEESLWRTQKVHRRCDYXNTLDSUPXMQTQDFAQAPYBGXPOLOCLFQNGNKPKOBHZWHRXAWAWJKMTJSLDLNHMUGVVOPSAMRUJEYUOBPFNEHPZZCLPNZKWMTCXERPZRFKSXVEZTYCXFRHRGEITWHRRYPWSVAYBUHCERJXDCYAVICPTNBGIODLYLMEYLISEYNXNMCDPJJRCTLYNFMJZQNCLAGHUDVLYIGASGXSZYPZKLAWQUDVNTWGFFYFFSMQWUNUPZRJMTHACFELGHDZEJWFDWVPYOZEVEJKQWHQAHOCIYWGVLPSHFESCGEUCJGYLGDWPIWIDWZZXRUFXERABQJOXZALQOCSAYBRHXQQGUDADYSORTYZQPWGMBLNAQOFODSNXSZFURUNPMZGHTAJUJROIGMRKIZHSFUSKIZJJTLGOEEPBMIXISDHOAIFNFEKKSLEXSJLSGLCYYFEQBKIZZTQQXBQZAPXAAIFQEIXELQEZGFEPCKFPGXULLAHXTSRXDEMKFKABUTAABSLNQBNMXNEPODPGAORYJXCHCGKECLJVRBPRLHORREEIZOBSHDSCETTTNFTSMQPQIJBLKNZDMXOTRBNMTKHHCZQQMSLOAXJQKRHDGZVGITHYGVDXRTVBJEAHYBYRYKJAVXPOKHFFMEPHAGFOOPFNKQAUGYLVPWUJUPCUGGIXGRAMELUTEPYILBIUOCKKUUBJROQFTXMZRLXBAMHSDTEKRRIKZUFNLGTQAEUINMBPYTWXULQNIIRXHHGQDPENXAJNWXULFBNKBRINUMTRBFWBYVNKNKDFR';
var needle = 'ABCDA'; // the order of letters doesn't matter
var letters = {};
needle.split('').forEach(function(ch) {
letters[ch] = letters[ch] || 0;
letters[ch]++;
});
var shortestSubstringLength = haystack.length;
var shortestSubstrings = []; // storage for found substrings
var startingPos = 0;
var length;
var currentPos;
var notFound;
var letterKeys = Object.keys(letters); // unique leters
do {
lettersLeft = JSON.parse(JSON.stringify(letters)); // copy letters count object
notFound = false;
posStart = haystack.length;
posEnd = 0;
letterKeys.forEach(function(ch) {
currentPos = startingPos;
while (!notFound && lettersLeft[ch] > 0) {
currentPos = haystack.indexOf(ch, currentPos);
if (currentPos >= 0) {
lettersLeft[ch]--;
posStart = Math.min(currentPos, posStart);
posEnd = Math.max(currentPos, posEnd);
currentPos++;
} else {
notFound = true;
}
}
});
if (!notFound) {
length = posEnd - posStart + 1;
startingPos = posStart + 1; // starting position for next iteration
}
if (!notFound && length === shortestSubstringLength) {
shortestSubstrings.push(haystack.substr(posStart, length));
}
if (!notFound && length < shortestSubstringLength) {
shortestSubstrings = [haystack.substr(posStart, length)];
shortestSubstringLength = length;
}
} while (!notFound);
console.log(shortestSubstrings);
Maybe not as clear as using regex could be (well, for me regex are never really clear :D ) you can use brute force (not so brute)
Create an index of "valid" points of your string (those with the letters you want) and iterate with a double loop over it getting substrings containing at least 5 of those points, checking that they are valid solutions. Maybe not the most efficient way, but easy to implement, to understand, and probably to optimize.
var haystack="UGDVWUDDPRQLUZMSZOJPSIKAIHLTONYXAULECXXKWFQOIKELWOHRVRUCXIAASKHMWTMAJEWGEESLWRTQKVHRRCDYXNTLDSUPXMQTQDFAQAPYBGXPOLOCLFQNGNKPKOBHZWHRXAWAWJKMTJSLDLNHMUGVVOPSAMRUJEYUOBPFNEHPZZCLPNZKWMTCXERPZRFKSXVEZTYCXFRHRGEITWHRRYPWSVAYBUHCERJXDCYAVICPTNBGIODLYLMEYLISEYNXNMCDPJJRCTLYNFMJZQNCLAGHUDVLYIGASGXSZYPZKLAWQUDVNTWGFFYFFSMQWUNUPZRJMTHACFELGHDZEJWFDWVPYOZEVEJKQWHQAHOCIYWGVLPSHFESCGEUCJGYLGDWPIWIDWZZXRUFXERABQJOXZALQOCSAYBRHXQQGUDADYSORTYZQPWGMBLNAQOFODSNXSZFURUNPMZGHTAJUJROIGMRKIZHSFUSKIZJJTLGOEEPBMIXISDHOAIFNFEKKSLEXSJLSGLCYYFEQBKIZZTQQXBQZAPXAAIFQEIXELQEZGFEPCKFPGXULLAHXTSRXDEMKFKABUTAABSLNQBNMXNEPODPGAORYJXCHCGKECLJVRBPRLHORREEIZOBSHDSCETTTNFTSMQPQIJBLKNZDMXOTRBNMTKHHCZQQMSLOAXJQKRHDGZVGITHYGVDXRTVBJEAHYBYRYKJAVXPOKHFFMEPHAGFOOPFNKQAUGYLVPWUJUPCUGGIXGR";
var needle="ABCD";
var size=haystack.length;
var candidate_substring="";
var minimal_length=size;
var solutions=new Array();
var points=Array();
for(var i=0;i<size;i++){
if(needle.indexOf(haystack[i])>-1) points.push(i);
}
var limit_i= points.length-4;
var limit_k= points.length;
for (var i=0;i<limit_i;i++){
for(var k=i;k<limit_k;k++){
if(points[k]-points[i]+1<=minimal_length){
candidate_substring=haystack.substr(points[i],points[k]-points[i]+1);
if(is_valid(candidate_substring)){
solutions.push(candidate_substring);
if(candidate_substring.length < minimal_length) minimal_length=candidate_substring.length;
}
}
}
}
document.write('<p>Solution length:'+minimal_length+'<p>');
for(var i=0;i<solutions.length;i++){
if(solutions[i].length<=minimal_length) document.write('<p>Solution:'+solutions[i]+'<p>');
}
function is_valid(candidate_substring){
//verify we've got all characters
for(var j=0;j<candidate_substring.length;j++){
if(candidate_substring.indexOf(needle.charAt(j))<0) return false;
}
//...and verify we have two "A"
if(candidate_substring.indexOf("A")==candidate_substring.lastIndexOf("A")) return false;
return true;
}
Just had this problem in an interview as a coding assignment and came up with another solution, (it's not as optimal as the one above but maybe it's easier to understand).
function MinWindowSubstring(strArr) {
const N = strArr[0];
const K = strArr[1];
const letters = {};
K.split('').forEach( (character) => {
letters[character] = letters[character] ? letters[character] + 1 : 1;
});
let possibleSequencesList = [];
const letterKeys = Object.keys(letters);
for(let i=0; i< N.length; i++) {
const char = N[i];
if (new String(letterKeys).indexOf(char) !== -1) {
// found a character in the string
// update all previus sequences
possibleSequencesList.forEach((seq) => {
if(!seq.sequenceComplete) {
seq[char] = seq[char]-1;
seq.lastIndex = i;
// check if sequence is complete
var sequenceComplete = true;
letterKeys.forEach( (letter) => {
if(seq[letter] > 0) {
sequenceComplete = false;
}
});
seq.sequenceComplete = sequenceComplete
}
})
// create a new sequence starting from it
const newSeq = {
startPoint: i,
lastIndex: i,
sequenceComplete: false,
...letters
}
newSeq[char] = newSeq[char]-1;
possibleSequencesList.push(newSeq);
}
}
// cleanup sequences
let sequencesList = possibleSequencesList.filter(sequence => sequence.sequenceComplete);
let output = [];
let minLength = N.length;
// find the smalles one
sequencesList.forEach( seq => {
if( (seq.lastIndex - seq.startPoint) < minLength) {
minLength = seq.lastIndex - seq.startPoint;
output = N.substring(seq.startPoint, seq.lastIndex + 1);
}
})
return output;
}