Return the shortest substring containing all the characters in the set
I couldn't remove the ifs and the flag , so i thought about a diffrent aproach, 

Using regular expressions seem more reasonable aproach, you can have a function that returns all the possible combinations of the letters in the array, create regular expressions from the resulting combinations and match against the orignalStr, 



function perm(xs) {
  let ret = [];
  for (let i = 0; i &lt; xs.length; i = i + 1) {
    let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));

    if (!rest.length)
      ret.push([xs[i]]);
    else
      for (let j = 0; j &lt; rest.length; j = j + 1)
        ret.push([xs[i]].concat(rest[j]));
  }
  return ret;
}

const originalStr = 'figehaeci', letters = ['a', 'e', 'i'];
let result = originalStr;

const combinations = perm(letters).map(combo =&gt; combo.join('.*?'));

// combinations : ["a.*?e.*?i","a.*?i.*?e","e.*?a.*?i","e.*?i.*?a","i.*?a.*?e","i.*?e.*?a"]

combinations.forEach(combo =&gt; {
  const exp = new RegExp(combo, 'g');
  const matches = originalStr.match(exp) || [];

  matches.forEach(match =&gt; {
    if (match.length &lt;= result.length)
      result = match;
  });
});

console.log(result);




The code you posted returns only one value if there's more substrings containing those letters, here's a tweak to store all possible shortest combinations in an array :



function perm(xs) {
  let ret = [];
  for (let i = 0; i &lt; xs.length; i = i + 1) {
    let rest = perm(xs.slice(0, i).concat(xs.slice(i + 1)));

    if (!rest.length)
      ret.push([xs[i]]);
    else
      for (let j = 0; j &lt; rest.length; j = j + 1)
        ret.push([xs[i]].concat(rest[j]));
  }
  return ret;
}

const originalStr = 'figehaecizaexi', letters = ['a', 'e', 'i'];
let shortestLength = originalStr.length, result = [];

const combinations = perm(letters).map(combo =&gt; combo.join('.*?'));

// combinations : ["a.*?e.*?i","a.*?i.*?e","e.*?a.*?i","e.*?i.*?a","i.*?a.*?e","i.*?e.*?a"]

combinations.forEach(combo =&gt; {
  const exp = new RegExp(combo, 'g');
  const matches = originalStr.match(exp) || [];

  matches.forEach(match =&gt; {
    if (match.length &lt; shortestLength) {
      shortestLength = match.length;
      result = [match];
    }
    if (match.length === shortestLength) {
      result.push(match);
    }
  });
});

const deduped = [...new Set(result)];

console.log(deduped);