Skip to content

Instantly share code, notes, and snippets.

@hectorerb
Created July 19, 2019 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hectorerb/a5b49e866d30b9113f1d3118238f0e92 to your computer and use it in GitHub Desktop.
Save hectorerb/a5b49e866d30b9113f1d3118238f0e92 to your computer and use it in GitHub Desktop.
JavaScript Coding Exemplar 2018 - Resolved

Answer 1

function validateString(str) {
    if (str.toLowerCase().indexOf('superman') === -1) {
        throw new Error('String does not contain superman');
    }
}

The indexOf () method returns the first index in which a given element can be found string, as "superman" is at the beginning of the string the index it is 0 and does not meet the condition

Answer 2

function isInArray(array, value) {
  return array.includes(value);
}

Using ES6 The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Answer 3

function formatPhoneNumber(phoneNumber, separator="-") {
    var phoneRegex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

    if (phoneRegex.test(phoneNumber)) {
      return phoneNumber.replace(phoneRegex, `$1${separator}$2${separator}$3`)
    } else {
      throw new Error('Invalid phone number');
    }
}

Using regular expressions allows validating the telephone number, after passing the validation the separator is added

Answer 4

To build unit tests in javascript it is recommended to use a library like Jest, MochaJS or Jasmine

In this case I create the test using an if statement, the function should always return the same result with these arguments

if ( this.fizzBuzz() === '12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz3132Fizz34BuzzFizz3738FizzBuzz41Fizz4344FizzBuzz4647Fizz49BuzzFizz5253FizzBuzz56Fizz5859FizzBuzz6162Fizz64BuzzFizz6768FizzBuzz71Fizz7374FizzBuzz7677Fizz79BuzzFizz8283FizzBuzz86Fizz8889FizzBuzz9192Fizz94BuzzFizz9798FizzBuzz') {
      console.log("test passed")
    } else {
      throw new Error('test failed');
    }

Answer 5

const files = [
  '/src/common/api.js',
  '/src/common/preferences.js',
  '/src/styles/main.css',
  '/src/styles/base/_base.scss',
  '/src/assets/apple-touch-icon-57.png',
];

const exclude = [
  '/src/styles/',
  '/src/assets/apple-touch-icon-57.png',
];

filesIncluded = (files, exclude) => {
    let filesToInclude = files.filter( (item) => {
      return this.shouldInclude(exclude, item)
    } );

    return filesToInclude
}

shouldInclude = (exclude, item, separator="") => {

    if(exclude.includes(`${item}${separator}`)) { return false }

    let arrayItem = item.split("/")

    if(arrayItem.length > 1) {

      arrayItem.pop()
      let strItem = arrayItem.join("/")
      return this.shouldInclude(exclude, strItem, separator="/")
    }

    return true
}

console.log(this.filesIncluded(files, exclude))

Answer 6

function getColorFromName(name) {
  var hash = 0;
    for (var i = 0; i < name.length; i++) {
       hash = name.charCodeAt(i) + ((hash << 5) - hash);
    }
    var color = (hash & 0x00FFFFFF)
        .toString(16)
        .toUpperCase();

    return "#"+"00000".substring(0, 6 - color.length) + color;
}

Answer 7

The querySelectorAll() method returns all elements in the document that matches a specified selector. The nodes can be accessed by index numbers

clicks the first button

You clicked button #0

clicks the the fourth button

You clicked button #3

When the elements are added to the DOM these are assigned in index number, The index starts at 0

Answer 8

function isIterable(item) {
    return (
      Array.isArray(item) ||
      (!!item && typeof item === "string") ||
      (!!item && typeof item === "object")
    );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment