Test2

by Samuel Guimarães

JavaScript

/*
- Criar objeto com cada palavra digitada originalmente
- Aplicar regras e criar uma string "formatada"

- Criar objecto com cada palavra do dicionário
- Aplicar regras e criar uma string "formatada"

-Pegar o array de objetos do dicionário e percorrer
-Comparar se a string formatada do dicionário é igual a string formatada do input

-Se for igual, adicionar a palavra original do dicionário dentro do objeto com a palavra original do input
*/

// Add contains method to array prototype
Array.prototype.contains = function(obj) {
  return this.indexOf(obj) > -1;
};

var dictionary = [
  'angel',
  'brave',
  'Braev',
  'Don',
  'Engel',
  'go',
  'goal',
  'son',
  'sunny',
  'Tom',
  'Tooonnnnyyyy'
];

var input = [
  '1ton#',
  'brief',
  'soon'
];

var formattedDictionary = [];
var formattedInput = [];
var formats = [
  ['a', 'e', 'i', 'o', 'u'],
  ['c', 'g', 'j', 'k', 'q', 's', 'x', 'y', 'z'],
  ['b', 'f', 'p', 'v', 'w'],
  ['d', 't'],
  ['m', 'n']
];

var output = {};

function applyRules(str) {
  // Rule 1 - Remove special characters and numbers
  str = str.replace(/(.)(?=.*\1)/g, '');
  // Has to run again to ensure that # is not passed
  str = str.replace(/[^a-z]/gi, '');

  // Rule 2 - Case insensitive
  str = str.toLowerCase();

  // Rule 3 - Slash character after second position if A, E, I, H, O, U, W or Y
  var remchars = str.substring(1, str.length);
  remchars = remchars.replace(/[aeihouwy]/ig, '');
  str = str[0] + remchars;

  // Rule 4 - Apply formats
  for (var i = 0, len = str.length; i < len; i++) {
    if (formats[0].contains(str[i])) {
      var str = str.substring(0, i) + formats[0][0] + str.substring(i + 1);
    }
    if (formats[1].contains(str[i])) {
      var str = str.substring(0, i) + formats[1][0] + str.substring(i + 1);
    }
    if (formats[2].contains(str[i])) {
      var str = str.substring(0, i) + formats[2][0] + str.substring(i + 1);
    }
    if (formats[3].contains(str[i])) {
      var str = str.substring(0, i) + formats[3][0]...