알고리즘 123 - Strip Comments [CodeWars]

박진현·2021년 12월 21일
0

알고리즘 (Javascript / C)

목록 보기
123/125

Q.

Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out.

Example:

Given an input string of:

apples, pears # and bananas
grapes
bananas !apples
The output expected would be:

apples, pears
grapes
bananas
The code would be called like so:

var result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"])
// result should == "apples, pears\ngrapes\nbananas"

A)

function solution(input, markers) {
  const myFunction = (input,index) => {
    return input.map((el) => {
      if(el.indexOf(markers[index]) >= 1) {
        return el.slice(0, el.indexOf(markers[index])).trim()
      }
      return el
    })
  }
  
  return myFunction(myFunction(input.split('\n'),0),1).join('\n')
};

오랜만에 4kyu문제 풀려니까 좀 오래걸린다. 뇌를 더 굴려야지...!!!!

profile
👨🏻‍💻 호기심이 많고 에러를 좋아하는 프론트엔드 개발자 박진현 입니다.

0개의 댓글