본문 바로가기
JavaScript/메소드

[javascript] 특정 문자 포함 단어 제거하기

by Angry Stock 2023. 7. 1.
반응형
function solution1(string) {
  var answer = [];
  string.map((a) => {
    if (a.indexOf('Hat') == -1) {
      answer.push(a);
    }
  });
  return answer;
}
function solution2(string) {
  return string.filter((a) => {
    return a.indexOf('Hat') == -1;
  });
}

const string = [
  'Apple Banana Cat',
  'Car Dance Hat',
  'Chocolate Grammar Mat',
  'Lake Apple Dance',
  'Banana Cat Hat',
  'Chocolate Lake Mat',
  'Car Apple Hat',
  'Dance Lake Mat',
  'Cat Chocolate Dance',
  'Grammar Hat Apple',
];

console.log('solution1', solution1(string));
console.log('solution2', solution2(string));
반응형

댓글