CoDEVIANT #11 (4/5/19)

autocomplete(‘ai’, [‘airplane’,’airport’,’apple’,’ball’]) = [‘airplane’,’airport’]
function autocomplete(input, dictionary){let array = [];let regexRule = /[A-Z]/ig;input = input.match(regexRule).join(‘’);dictionary.forEach((word)=>{if(word.slice(0,input.length).toLowerCase() === input){if(array.length <=4){array.push(word);}}})return array;}
  • I make an empty array…called array.
  • I create a variable that contains a regex rule (which I called regexRule) which seeks for all alphabetical characters a through z globally (hence the g) which means it will catch all the matches, and with ignoring the case of the letter it encounters (hence the i).
  • I make input equal itself after going through the string method .match( ) with the regexRule we created as an argument.
  • This method (.match( ) ) creates an array, so I immediately chain the array method .join(‘’) onto it so that I can get a string.
  • At this point, input is now free of any special characters.
  • I use the .forEach( ) method on the argument we were passed called dictionary, an array with tons of words.
  • Inside the method’s code block, I create an if statement where i seek that the word, truncated down to the length of the input with the use of .slice( ) a string method that returns portions of strings based on the arguments you pass into it [in our case I went from the beginning 0 to however long the input was input.length], AND made lowercase with .toLowerCase( ) equals what we have for input
  • If that’s the case:
  • If the array’s length is less then or equal to 4, we push word into the array.
  • If that’s the case and array has a length of 5 OR if it’s not true
  • Nothing happens
  • We return array.
function autocomplete(input, dictionary){var r = new RegExp(‘^’ + input.replace(/[^a-z]/gi,’’), ‘i’);return dictionary.filter(function(w){ return r.test(w); }).slice(0, 5);}
  • We make a variable r which uses the RegEx symbol of the carrot ^ to denote the beginning of string the RegEx pattern will be used on. Then we concatenate the result of using the .replace( ) string method on input but we pass in a RegEx Expression and a pair of empty quotes. This is going to result in us getting the replace( ) method’s expression -> { a cocktail of getting from the very beginning the a-z alphabetical characters of input regardless of case (i for ignore-case), throughout the entire length of input (g for global), then we concatenate an ‘i’ at the end.
  • The result is something like that that will be used in the next step of our function:
  • Next we return the dictionary argument with the array method .filter( ) being run on it. It has a callback function of taking each element of dictionary and calling it w. then we return inside the function’s code block r testing the passed in w to see if it’s beginning characters match it. Then we slice this array that is being returned from the filter from 0 to 5.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adrian Rosales

Adrian Rosales

is a web developer, opera singer, actor, and lover of cats. (