Pages

Sunday, June 19, 2016

Coding challenge for Underscore functions

Take an array of names, and return a new array with no names repeated.

For example :

var arrayNames = ["Abagail", "Andrew", "Andrew", "Betsy", "Bobby", "Carl", "Cliff", "Carl"];

should return = ["Abagail", "Andrew", "Betsy", "Bobby", "Carl", "Cliff"];


Solution is in the comments.



Extra credit : Build a function that will count how many times the same names appears in the array.

The function fed arrayNames should return 2, because 2 times a name appears that is already in the array.

Solution is also in the comments.

2 comments:

  1. var example = _.uniq(arrayNames);

    ReplyDelete
  2. var doubleCounter = function(array){

    return array.length - _.uniq(array).length ;
    }

    ReplyDelete