My new code snippet!

Check out my awesome coding creation from the Grasshopper Gallery!

Snippet Name: Subsequence

Description: Hi there, @Grasshopper_Ben. I made this code snippet today. This is my own code. Hope you get impressed. :smiley:

Your code so far:

let dictionaryWord = pickRandom(['lull', 'hub', 'boo', 'quack', 'balloon', 'hall']);
function findNextIndex(mappedIndices, lastMatchedIndex) {
    for (var index of mappedIndices) {
        if (index >= lastMatchedIndex) {
            return index + 1;
        }
    }
    return false;
};
const makeMap = (o) => {
    var out = '';
    for (var p in o) {
        out += p + ': [' + o[p] + ']' + ', ';
        console.log(out);
        out = '';
    }
    return o;
};
let stringSequence = {
    h: [0],
    u: [1],
    l: [2, 3, 7, 8],
    a: [4, 6],
    b: [5],
    o: [9, 10]
};
console.log(dictionaryWord);
let stringSequenceObject = makeMap(stringSequence);
function isSubsequence(word, map) {
    let minIndex = 0;
    for (var letter of word) {
        if (map[letter]) {
            minIndex = findNextIndex(map[letter], minIndex);
            if (minIndex = false) {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
};
console.log(isSubsequence(dictionaryWord, stringSequenceObject));

@Grasshopper_Ben, you might get impressed it! :smiley: :smiley:
Thank you!
Pummarin :smile:

1 Like