Sequence Search given a key

Determine if the characters of the key appear in order (although, not necessarily contiguously) in the sequence. My code is returning true for all of them and I can't figure out what to do.

function sequenceSearch(sequence, key) {
    for (var i = 0; i < key.length; i++) {
        if (!sequence.includes(key.charAt(i))) {
            return false;
    return true;

console.log(sequenceSearch('arcata', 'cat')); //true
console.log(sequenceSearch('c1a2t3', 'cat')); //true
console.log(sequenceSearch('cta', 'cat')); //false
console.log(sequenceSearch('caat', 'cat')); //true

Read more here:

Content Attribution

This content was originally published by Ryan Coon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: