Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Question 1
- This occurs because indexOf can return 0. if (0) is falsy and therefore does not pass.
- To change this, we may change the code to the following
- ```js
- function validateString(str) {
- if (!str.toLowerCase().indexOf('superman') != -1) {
- throw new Error('String does not contain superman');
- }
- }
- ```
- indexOf returns -1 if it couldn't find the occurence of the string. Therefore, we can confidently do a if != -1 statement.
- ### Question 2
- ES6 has introduced the Array.prototype.includes which checks if an element is in an array.
- ```js
- return array.includes(element)
- ```
- However this is not the optimal answer as the runtime of this would be at worst case O(n), n being the number of elements in the array.
- Since the array is sorted, we could run a binary search on it, searching from the middle and searching the left or right side if element is bigger or smaller than the middle element.
- It is easy to find a binary search code online. The worst case runtime of the above binary search would be O(log n), n being the number of elements in the array.
- ### Question 3
- ```js
- const formatPhoneNumber = (phoneNumber, delimiter) => {
- // remove all non digit characters
- sanitizedNumber = phoneNumber.replace(/\D/g,'');
- if (sanitizedNumber.length != 10) {
- throw new Error('the supplied phone number does not contain 10 digits');
- }
- if (!delimiter) {
- delimiter = '-';
- }
- return `${sanitizedNumber.substring(0,3)}${delimiter}${sanitizedNumber.substring(3,6)}${delimiter}${sanitizedNumber.substring(6)}`;
- }
- ```
- ### Question 4
- Tests written in Jest
- ```js
- expect(fizzBuzz(-1, -1)).toThrow(new Error('Invalid arguments'));
- expect(fizzBuzz(-1, 3)).toThrow(new Error('Invalid arguments'));
- expect(fizzBuzz(5, -1)).toThrow(new Error('Invalid arguments'));
- expect(fizzBuzz(0, 0)).toThrow(new Error('Invalid arguments'));
- expect(fizzBuzz(10, 1)).toThrow(new Error('Invalid arguments'));
- expect(fizzBuzz(0, 15).toEqual("12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz");
- expect(fizzBuzz(15, 15).toEqual("FizzBuzz");
- ```
- ### Question 5
- My approach to handling this problem without using a nested for loop would be to:
- - create a tree out of the files.
- eg:
- ```js
- 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',
- ];
- ```
- would become
- ```js
- const files = {
- src: {
- common: {
- 'api.js': true,
- 'preferences.js': true,
- },
- styles: {
- 'main.css': true,
- base: {
- '_base.scss': true
- }
- },
- assets: {
- 'apple-touch-icon-57.png': true
- }
- }
- }
- ```
- By doing this, i could easily traverse the tree for each item in the excluded list.
- If the exclude is a directory, i can easily delete that key from my file list tree and all of the files under it will be deleted as well.
- This would change the runtime from the nested for each being O(n * m), n being length of file array and m being length of exclude array, to O(n + m * d).
- n to construct file tree, m to go through exclude list and remove from file tree and s being the maximum depth of our file system.
- ### Question 6
- For this question, i decided to hash the name into a decimal by adding all the character codes of each character in the name
- to the power of 5 and adding 1 at the end. I then modulo 16777215 to the result (this is the max hex color code in decimal #ffffff).
- I then convert the hash decimal into a hex number using the dec2hex function.
- ```js
- const getColorFromName = (name) => {
- const hexColor = dec2hex(hashName(name));
- return hexColor;
- }
- const hashName = (name) => {
- const maxHexDecimal = 16777215;
- return [...name].reduce((a, b) => {
- return a + Math.pow(b.charCodeAt(0), 5);
- }, 1) % maxHexDecimal;
- }
- function dec2hex(i) {
- return (i+0x100000).toString(16).substr(-6).toUpperCase();
- }
- ```
- ### Question 7
- If the user clicks on the first and fourth button,
- ```
- You clicked button #0
- You clicked button #3
- ```
- will be printed on the console.
- ### Question 8
- ```js
- const isIterable = (object) => {
- if (object == null) return false;
- return typeof object[Symbol.iterator] === 'function';
- }
- ```
Add Comment
Please, Sign In to add comment