Discussion, Questions, and Resources for Part 3 of the April 2018 Cohort (Intermediate Algorithm Scripting and JavaScript Algorithms and Data Structures Projects)

Here are the next three. :sunny:

Missing Letters

I matched the string to a string containing every letter of the alphabet, then looped through each letter and placed the missing letter in a result variable.

function fearNotLetter(str) {
  const letters = "abcdefghijklmnopqrstuvwxyz";
  let letterArr = letters.split('').splice(letters.indexOf(str[0]), str.length + 1);
  let counter = 0;
  let result = [];

  letterArr.map(letter => {
    if (str[counter] !== letter) result.push(letter);
    counter++;
  });

  return result[0];
}

The fCC Guide for Missing Letters compares letters using character codes with charCodeAt():

function fearNotLetter(str) {
  for(var i = 0; i < str.length; i++) {
    var code = str.charCodeAt(i);

    /*
       If the current character code does not equal the current letter's
       character code, then return the previous character code.
     */
    if (code !== str.charCodeAt(0) + i) {
      return String.fromCharCode(code - 1);
    }  
  }
  return undefined;
}

It’s less code and doesn’t need all of the variables that my solution needs. From that, I was able to come up with this using filter():

function fearNotLetter(str) {
  let firstCharacter = str.charCodeAt();
  
  let notEqual = str.split('').filter((letter, letterIndex) => letter !== String.fromCharCode(firstCharacter + letterIndex))[0];

  return notEqual === undefined ? undefined : String.fromCharCode(notEqual.charCodeAt() - 1);
}

I’m not sure which way is best. They all work.

Sorted Union

I solved this one using map(), Set(), and the spread operator:

function uniteUnique(arr) {
  let allArrays = [];
  [...arguments].map(insideArr => allArrays.push(...insideArr));

  return [...new Set(allArrays)];
}

Apparently, this can be done with even less code, according to the fCC Guide on Sorted Union:

function uniteUnique(arr) {

  //make an array out of arguments and flatten it (using the spread operator)
  const args = [].concat(...arguments);

  // create a Set
  return [...new Set(args)];
}
Convert HTML Entities

I used regular expressions and replace(), which almost matches the intermediate code solution in the fCC Guide for Convert HTML Entities:

function convertHTML(str) {
  return str.replace(/&/g, '&amp;')
            .replace(/>/g, '&gt;')
            .replace(/</g, '&lt;')
            .replace(/'/g, '&apos;')
            .replace(/"/g, '&quot;');
}

The guide also has a solution that uses an object. I sensed this was possible and attmepted it, but didn’t solve it that way. I did modify the guide’s solution to use an arrow function:

function convertHTML(str) {
  const htmlEntities={
    '&':'&amp;',
    '<':'&lt;',
    '>':'&gt;',
    '\"':'&quot;',
    '\'':"&apos;"
  };

  return str.split('').map(entity => htmlEntities[entity] || entity).join('');
}

Either way seems to work. It’s trivial to add more properties/conditions for both solutions, however, the object-oriented solution seems to be more flexible.