JSFiddle - React, Tailwind, and code Playground

by jacobwsmith

JavaScript

'use strict';

/*
 * THE TASK:
 * Return a sorted list of the unique names. We need a method that will perform this functionality.  
 * The results of this method should be output to an html page as well as the console.log
 *
 * Assumptions
 * - This example is sorting ASC
 * - In this example unique means removing capitalization, since we are dealing with names we assume all are strings.
 *
 */

// Return a sorted list of the unique names.
function proccessArray(namesList) {
  return namesList
    .map(function(item) {
      if (typeof item === 'string') {
        return item.toLowerCase();
      }
      // TODO: check if number or object or array
      return item.toString();
    })
    .sort()
    .filter(function(item, index, arr) {
      return !index || item !== arr[index - 1];
    })
}

// The results of this method should be output to an html page as well as the console.
(function() {

  // Array to Test
  var a = ['Nick', 'jake', 'RAY', 'Kate', 'Nick', 'Jeremy', 'nick', 'AMOL', 'rAY', 'VIANNEY', 'Shilpika', 'nick', 'THOMAS', 'tom', 'james', 'JERM', 'amOl', 'kate', 'SCOTT', 'Jenifer', 'bill', 'jenny', 'STEVEN'];
  
  // Results to output
  var results = proccessArray(a);

  // Append paragraph to body. This paragarph will hold a comma seperated list.
  var p = document.createElement("p");
  p.innerHTML = results.join(', ');
  document.body.appendChild(p);

  // Output Results To Console 
  console.log(results.join(', '));

}());