-1

I can use array.splice to remove any element from the array

I am specifically looking @ multiples of 3 and 5 in the array

Multiples of X should be removed

number 3 and number 5 multiples What is the method i need to write for this. I need to use Vanilla Javascipt

var myArray = [1,2,3,5,9,15,16,21,23,25];

            function removeMultiples(ary, num) {



            }

            //removeMultiples(myArray, 3) => [1,2,5,16,23,25]
            //removeMultiples(myArray, 5) => [1,2,3,9,16,21,23]
          </script>
Insane Fragger
  • 65
  • 1
  • 12
  • 1
    You need **iterate** over the array. You can do that using a [`for` loop](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for). Which part specifically are you having problems with? We are happy to help you fix your code but we are not writing the code for you. – Felix Kling May 18 '16 at 23:39
  • Possible duplicate of [Remove a particular element from an array in JavaScript?](http://stackoverflow.com/questions/5767325/remove-a-particular-element-from-an-array-in-javascript) – AlwaysNull May 18 '16 at 23:40
  • You can also do it using [.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Patrick Evans May 18 '16 at 23:41
  • I can use array.splice to remove any element from the array I am specifically looking @ multiples of 3 and 5 in the array – Insane Fragger May 18 '16 at 23:45

3 Answers3

2

Prob a good use for filter:

Working Example

function removeMultiples(arr, mul) {
  return arr.filter(function(el) {
    return el % mul !== 0;
  })
}

Or with a for loop:

function remove(arr, mul) {
  var result = [];
  for (var i = 0; i < arr.length; i++) {
    if (arr[i] % mul) {
      result.push(arr[i]);
    }
  }
  return result;
}
omarjmh
  • 11,814
  • 6
  • 29
  • 40
2

Description

Given a comma delimited string if multi digit numbers or a string with a single multi-digit number, you could use this regex to replace the leading comma and the numbers evenly divisible by 3 or 5 with null.

(?:,|^)((?:[0369]|[258][0369]*[147]|[147](?:[0369]|[147][0369]*[258])*[258]|[258][0369]*[258](?:[0369]|[147][0369]*[258])*[258]|[147](?:[0369]|[147][0369]*[258])*[147][0369]*[147]|[258][0369]*[258](?:[0369]|[147][0369]*[258])*[147][0369]*[147])*|[0-9]*[50])(?=,|\Z)

a more detailed explanation of this expression can be found here: see also link

Replace with: nothing

Regular expression visualization

Example

Live Demo

https://regex101.com/r/iG9lP4/1

Sample string

1,2,3,5,9,15,16,21,23,25

After replacement

1,2,16,23

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    ,                        ','
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    ^                        the beginning of a "line"
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      (?:                      group, but do not capture (0 or more
                               times (matching the most amount
                               possible)):
----------------------------------------------------------------------
        [0369]                   any character of: '0', '3', '6', '9'
----------------------------------------------------------------------
       |                        OR
----------------------------------------------------------------------
        [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
        [0369]*                  any character of: '0', '3', '6', '9'
                                 (0 or more times (matching the most
                                 amount possible))
----------------------------------------------------------------------
        [258]                    any character of: '2', '5', '8'
----------------------------------------------------------------------
      )*                       end of grouping
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
      [0369]*                  any character of: '0', '3', '6', '9'
                               (0 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
      [147]                    any character of: '1', '4', '7'
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [0-9]*                   any character of: '0' to '9' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    [50]                     any character of: '5', '0'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    ,                        ','
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    \Z                       before an optional \n, and the end of
                             the string
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
Ro Yo Mi
  • 13,586
  • 4
  • 31
  • 40
  • Well this certainly is a *different* solution... I like seeing other ways to solve problems but I'm afraid this is pretty unmaintainable. It's nearly impossible to verify that this is working correctly for all natural numbers. I suppose the solution for 5 would be a little easier tho, `/\d*[05]/`. – Thank you May 19 '16 at 03:39
  • Oh I totally agree, this isn't easily maintainable, but the OP did ask for a regular expression that would match numbers evenly divisible by three, and at least in the live demo it does work for as expected and it's theory of operation is pretty thoroughly vetted. – Ro Yo Mi May 19 '16 at 04:43
  • 1
    I totally missed the part where he/she asked for a regular expression – Thank you May 19 '16 at 04:50
1

This sounds like a homework assignment. Here's a homeworkish answer that doesn't rely on the Array.prototype.filter built-in method. This simple recursive definition uses constant time and space.

const removeMultiples = (n, xs)=> {
  let loop = (ys, xs)=> {
    if (xs.length === 0)
      return ys;
    else if (xs[0] % n === 0)
      return loop(ys, xs.slice(1));
    else
      return loop(ys.concat([xs[0]]), xs.slice(1));
  };
  return loop([], xs);
};

removeMultiples(3, [0,1,2,3,4,5,6,7,8,9]);
//=> [1,2,4,5,7,8]

This uses Array.prototype.filter and probably doesn't teach you anything.

myArray.filter(x=> x % 3 !== 0);
myArray.filter(x=> x % 5 !== 0);

This uses combinations of simple functions to achieve your goal. The benefit of this approach is that each function does a single, simple thing, and each function is highly reusable.

// (b->c) -> (a->b) -> (a->c)
const comp = f=> g=> x=> f (g (x));

// Boolean -> Boolean
const not = x=> !x;

// Number -> Number -> Boolean
const isDivisibleBy = x=> y=> y % x === 0;

// Array -> Boolean
const isEmpty = xs=> xs.length === 0;

// [Value] -> Value
const first = xs=> xs[0];

// [Value] -> [Value]
const rest = xs=> xs.slice(1);

// [Value] -> [Value] -> [Value]
const concat = xs=> ys=> ys.concat(xs);

// Value -> [Value] -> [Value]
const append = x=> concat([x]);

// (a->b->a) -> a -> [b] -> a
const reduce = f=> y=> xs=>
  isEmpty(xs) ? y : reduce (f) (f (y) (first (xs))) (rest (xs));

// (Value->Boolean) -> [Value] -> [Value]
const filter = f=>
  reduce (ys=> x=> f (x) ? append (x) (ys) : ys) ([]);

// Number -> [Number] -> [Number]
const removeMultiples = x=>
  filter (comp (not) (isDivisibleBy(x)));

Ok so now let's see all the functions work together in harmony !

removeMultiples (3) ([1,2,3,5,9,15,16,21,23,25]);
//=> [ 1, 2, 5, 16, 23, 25 ]

removeMultiples (5) ([1,2,3,5,9,15,16,21,23,25]);
//=>  [ 1, 2, 3, 9, 16, 21, 23 ]
Thank you
  • 107,507
  • 28
  • 191
  • 224