Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Trying to create a simple keyword-based search bar in Javascript (Twine)

I'm trying to create a simple keyword-based search function in a Twine story using Javascript. I've got something functional using this guide, but I'm trying to modify it slightly. I'd like the search to output all matches, rather than just the highest one, or at least the top 5 highest matches. Can anyone help me with this? I'm unfortunately not very familiar with Javascript so trying to figure out what to change and then what to change it to has been very difficult.


window.SearchTerm = window.SearchTerm || function (h, descr, kw) {
    'use strict';
    
    if (this instanceof SearchTerm) {
        this.handle      = h;
        this.description = descr;
        this.keywords    = kw;
    } else {
        return new SearchTerm(h, descr, kw);
    }
};

SearchTerm.add = function (h, descr, kw) {
    termList.push(new SearchTerm(h, descr, kw));
};

SearchTerm.prototype = {
    
    compare : function (stringArray) {
        var term = this;
        var matches = (function () {
            var ret = 0;
            stringArray.forEach( function (str) {
                if (term.keywords.includes(str)) {
                        ret++;
                }
            });
            return ret;
        }());
        
        return matches;
        
    },
    
    constructor : window.SearchTerm
};

$(document).on('change', 'input[name=search-box]', function (e) {
    var string = $(this).val(),
            matches = [], i, n = -1, highest = 0, content;
    
    string = string.replace(/[^A-Za-z0-9\s]/, ' ');
    string = string.replace(/[\s\s+]/, ' ');
    string = string.trim();
    string = string.split(' ');
    
    termList.forEach( function (term) {
        matches.push(term.compare(string));
    });
    
    for (i = 0; i < matches.length; i++) {
        if (matches[i] > n) {
            n = matches[i];
            highest = i;
        }
    }
    
    if (n <= 0) {
        content = '';
    } else {
        content = termList[highest].handle + ': ' + termList[highest].description;
    }
    
    $('#output')
        .empty()
        .append(content);
});

// add your search terms here...
SearchTerm.add('Test', '#000', ['test', 'testing']);
SearchTerm.add('Test', '#001', ['test', 'testing']);
SearchTerm.add('Test', '#002', ['test', 'testing']);
// etc.
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda