JSFiddle - React, Tailwind, and code Playground

by CommandLineDesign

JavaScript

var AutoCompleteHandler = function(dictionary){
	this.dictionary = dictionary;
  this.matches = [];
  this.performSearch = function(input){
  	this.input = input.toUpperCase().replace(/[^A-Z]/g, '');
    this.inputLength = this.input.length
    this.input = new RegExp(this.input);
    for(var i = 0; i < this.dictionary.length; i++){
    	if(this.dictionary[i].slice(0, this.inputLength).toUpperCase().match(this.input) && this.matches.length < 5){
      	this.matches.push(this.dictionary[i]);
      }
    }
    return this.matches;
  }
}


function autocomplete(input, dictionary){
	  var myAutoComplete = new AutoCompleteHandler(dictionary);
    return myAutoComplete.performSearch(input);
}

var dictionary = [...