Created
May 15, 2019 21:20
-
-
Save alexbrohman/614274128c9c00d0473523122af67975 to your computer and use it in GitHub Desktop.
Alex Brohman Javascript Coding Exemplar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# JavaScript Coding Exemplar: | |
### Question 1 | |
The issue with the function is that when the string begins with 'superman', the indexOf is 0, which is a 'falsy' value. The if statement returns false for this 0 and because of this, the error is thrown. | |
The indexOf for a string without 'superman' is -1, so a better function in this case would be: | |
``` | |
const validateString = (str) => { | |
if(str.toLowerCase().indexOf('superman') === -1) | |
throw new Error('String does not contain superman') | |
} | |
``` | |
### Question 2 | |
My function takes both the integer and the array, checks the integer against the array and will return a true or false value. In my response I assumed a “sorted index array without keys only including integers” would look like one of these examples: | |
let arrOne = [1, 2, 3, 4, 5, 6, 7] | |
let arrTwo = [3, 51, 200, 1200, 9999] | |
My solution is to use the es6 includes() method because this seems like the situation to use it, as it is specifically used for checking whether a value exists within an array. | |
``` | |
const findInArray = (int, arr) => { | |
return arr.includes(int) | |
} | |
``` | |
### Question 3 | |
The function checks for a delimiter. If there is not one provided, it is set as a dash. | |
The number is sanitized of any non-integer characters and then converted into an array, split up into parts and turned back into a string with template literal syntax. | |
``` | |
formatPhoneNumber = (number, delimiter) => { | |
delimiter = delimiter ? delimiter : '-' | |
number = number.replace(/\D/g,'').split('') | |
p1 = number.slice(0, 3).join('') | |
p2 = number.slice(3,6).join('') | |
p3 = number.slice(6,10).join('') | |
return(`${p1}${delimiter}${p2}${delimiter}${p3}`) | |
} | |
//Examples: | |
formatPhoneNumber('(514)6593210', ':') | |
//=> 514:659:3210 | |
formatPhoneNumber('(514) 659 3210') | |
//=> 514-659-3210 | |
``` | |
### Question 4 | |
I have not yet learned about writing unit tests. It’s definitely in my personal learning roadmap. | |
If it’s any consolation, in my last position we relied heavily on prop-types in our React Application to be ensure we were being provided the right types of data for each of our components. | |
### Question 5 | |
This function first removes all the files matching the excluded directories, by checking to see if the substring up to the last ‘/‘ matches any of the directory names that should, then filters the remaining files of any matching file names. | |
``` | |
const excludeFiles = (files, exclude) => { | |
//get excluded directories | |
let exDir = exclude.filter(x => x.lastIndexOf('/') +1 == x.length) | |
let includedFiles = files | |
//remove files included in excluded directories | |
exDir.forEach( (ex) => { | |
includedFiles = includedFiles.filter(f => f.indexOf(ex)) | |
}) | |
//check each FILE for FILES that contain match with exdir | |
includedFiles = includedFiles.filter( file => exclude.indexOf(file) === -1) | |
console.log(includedFiles) | |
return includedFiles | |
} | |
excludeFiles(files, exclude); | |
``` | |
### Question 6 | |
To generate a hex value from a name, I decided to use the primary constants I knew I had available: | |
The person has a first and a last name, with a first and last letter. This gives me 4 value, even if each of the person’s names is only one letter long. I also know i have a length of the two names, even if it is only one. I now have 6 values that will give me at least 6 elements I can work with. | |
I get the values of the first and last name, as well as their corresponding lengths, put it in an array and return the first 6 values in a string. | |
Disappointingly, my own name gave me an unpleasant brown color. | |
``` | |
const getColorFromName = (name) => { | |
//hex codes go from 0 t 9, then a - f | |
const hexchars = 'abcdef'.split('') | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('') | |
//assuming user provides at least 2 names delimited with a space. Good news for James Earl Jones, bad news for Beyonce. | |
name = name.toLowerCase() | |
const firstName = name.slice(0, name.indexOf(' ')) | |
const lastName = name.slice(name.lastIndexOf(' ') + 1, name.length) | |
let hex = []; | |
const generateHexValue = (namePart) => { | |
let first = namePart[0]; | |
let last = namePart[namePart.length -1] | |
hexValFirst = hexchars.includes(first) ? first : alphabet.indexOf(first) | |
hexValLast = hexchars.includes(last) ? last : alphabet.indexOf(last) | |
return `${hexValFirst}${hexValLast}` | |
} | |
hex.push(generateHexValue(firstName)) | |
hex.push(generateHexValue(lastName)) | |
hex.push(firstName.length) | |
hex.push(lastName.length) | |
return `#${hex.join('').slice(0, 6)}` | |
} | |
const name = 'Alex Brohman'; | |
const color = getColorFromName(name); | |
``` | |
### Question 7 | |
Clicking on the first button will log “You clicked button #0 ” and clicking on the third will log “You clicked button #3 “. | |
This occurs because the iterator of ‘i’ starts at zero in the for loop. The value of the iterator is then provided to the message logged in the console when the user clicks that button. | |
### Question 8 | |
This function checks for null or undefined values to return false, and then checks for length to determine true. For other cases where the provided argument does not have a length, false is returned. | |
``` | |
isIterable = (arg) => { | |
if(arg === null || arg === undefined) | |
return false | |
else if (arg.length >= 0) | |
return(true) | |
else { | |
return false | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment