commonCharacters

Written on July 23, 2019

문제

Write a function f(a, b) which takes two strings as arguments and returns a string containing the characters found in both strings (without duplication), in the order that they appeared in a. Remember to skip spaces and characters you have already encountered!

Example: commonCharacters(‘acexivou’, ‘aegihobu’) Returns: ‘aeiou’

Extra credit: Extend your function to handle more than two input strings.

풀이

var commonCharacters = function(string1, string2) {
  let args = Array.from(arguments);
  let commonCharacterString = "";
  const isIncluded = str => {
    for (let i = 1; i < args.length; i++) {
      if (!args[i].includes(str)) {
        return false;
      }
    }
    return true;
  };
  for (let i = 0; i < args[0].length; i++) {
    if (isIncluded(args[0][i]) && !commonCharacterString.includes(args[0][i])) {
      commonCharacterString += args[0][i];
    }
  }
  return commonCharacterString;
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself