티스토리 뷰

  • 문제

    You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

     

  • 예시

    destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
    
    destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
    
    destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
    

 

  • 내가 푼 답

    function destroyer(arr) {
     
      for(var i = 0 ; i < arguments.length ; i++) {
        if(!Array.isArray(arguments[i])) {
          for(var j = 0 ; j < arr.length ; j++) {
            if(arr[j] === arguments[i]) {
              arr.splice(j,1);
    	  j = j - 1
            }
          }
        } 
      } return arr;
    }
    

 

  • Advanced Solution 1

    function destroyer(arr) {
      var args = Array.from(arguments).slice(1);
      return arr.filter(function(val) {
        return !args.includes(val);
      });
    }
    

     

  • Advanced Solution 2

    const destroyer = (arr, ...args) => arr.filter(i => !args.includes(i));
    

     


'Data Structure & Algorithm' 카테고리의 다른 글

분할정복  (0) 2019.03.28
JS_Pig Latin  (0) 2019.03.21
JS_Wherefore Art Thou  (0) 2019.03.21
JS_Diff Two Arrays  (0) 2019.03.20
JS_Sum All Numbers in Range  (0) 2019.03.20
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함