여러 단어를 가진 배열을 입력받아, 중복되지 않은 단어로만 구성하여 배열을 반환하시오.
<html>
<head>
<meta charset="UTF-8">
<title>출력결과</title>
</head>
<body>
<script>
function solution(s){
let answer =[];
s.filter((ele,idx)=>{
if(answer.indexOf(ele)==-1){
answer.push(ele);
}
})
return answer;
}
let str=["good", "time", "good", "time", "student"];
console.log(solution(str));
</script>
</body>
</html>
위 코드는 filter()함수와 indexOf()를 이용한다.
filter 함수를 통해 입력 배열을 돌면서 answer에 안들어있는 요소만 answer에 집어넣도록 한다.