Guest User

Untitled

a guest
Feb 17th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. ### Question 1
  2. This occurs because indexOf can return 0. if (0) is falsy and therefore does not pass.
  3. To change this, we may change the code to the following
  4. ```js
  5. function validateString(str) {
  6. if (!str.toLowerCase().indexOf('superman') != -1) {
  7. throw new Error('String does not contain superman');
  8. }
  9. }
  10. ```
  11. indexOf returns -1 if it couldn't find the occurence of the string. Therefore, we can confidently do a if != -1 statement.
  12.  
  13.  
  14. ### Question 2
  15. ES6 has introduced the Array.prototype.includes which checks if an element is in an array.
  16. ```js
  17. return array.includes(element)
  18. ```
  19. 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.
  20.  
  21. 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.
  22.  
  23. 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.
  24.  
  25.  
  26. ### Question 3
  27. ```js
  28. const formatPhoneNumber = (phoneNumber, delimiter) => {
  29. // remove all non digit characters
  30. sanitizedNumber = phoneNumber.replace(/\D/g,'');
  31.  
  32. if (sanitizedNumber.length != 10) {
  33. throw new Error('the supplied phone number does not contain 10 digits');
  34. }
  35.  
  36. if (!delimiter) {
  37. delimiter = '-';
  38. }
  39.  
  40. return `${sanitizedNumber.substring(0,3)}${delimiter}${sanitizedNumber.substring(3,6)}${delimiter}${sanitizedNumber.substring(6)}`;
  41. }
  42. ```
  43.  
  44. ### Question 4
  45. Tests written in Jest
  46. ```js
  47. expect(fizzBuzz(-1, -1)).toThrow(new Error('Invalid arguments'));
  48. expect(fizzBuzz(-1, 3)).toThrow(new Error('Invalid arguments'));
  49. expect(fizzBuzz(5, -1)).toThrow(new Error('Invalid arguments'));
  50. expect(fizzBuzz(0, 0)).toThrow(new Error('Invalid arguments'));
  51. expect(fizzBuzz(10, 1)).toThrow(new Error('Invalid arguments'));
  52. expect(fizzBuzz(0, 15).toEqual("12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz");
  53. expect(fizzBuzz(15, 15).toEqual("FizzBuzz");
  54. ```
  55.  
  56. ### Question 5
  57. My approach to handling this problem without using a nested for loop would be to:
  58. - create a tree out of the files.
  59. eg:
  60. ```js
  61. const files = [
  62. '/src/common/api.js',
  63. '/src/common/preferences.js',
  64. '/src/styles/main.css',
  65. '/src/styles/base/_base.scss',
  66. '/src/assets/apple-touch-icon-57.png',
  67. ];
  68. ```
  69. would become
  70. ```js
  71. const files = {
  72. src: {
  73. common: {
  74. 'api.js': true,
  75. 'preferences.js': true,
  76. },
  77. styles: {
  78. 'main.css': true,
  79. base: {
  80. '_base.scss': true
  81. }
  82. },
  83. assets: {
  84. 'apple-touch-icon-57.png': true
  85. }
  86. }
  87. }
  88. ```
  89.  
  90. By doing this, i could easily traverse the tree for each item in the excluded list.
  91. 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.
  92.  
  93. 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).
  94. 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.
  95.  
  96.  
  97. ### Question 6
  98. For this question, i decided to hash the name into a decimal by adding all the character codes of each character in the name
  99. 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).
  100. I then convert the hash decimal into a hex number using the dec2hex function.
  101.  
  102. ```js
  103. const getColorFromName = (name) => {
  104. const hexColor = dec2hex(hashName(name));
  105. return hexColor;
  106. }
  107.  
  108. const hashName = (name) => {
  109. const maxHexDecimal = 16777215;
  110.  
  111. return [...name].reduce((a, b) => {
  112. return a + Math.pow(b.charCodeAt(0), 5);
  113. }, 1) % maxHexDecimal;
  114. }
  115.  
  116. function dec2hex(i) {
  117. return (i+0x100000).toString(16).substr(-6).toUpperCase();
  118. }
  119. ```
  120.  
  121. ### Question 7
  122. If the user clicks on the first and fourth button,
  123. ```
  124. You clicked button #0
  125. You clicked button #3
  126. ```
  127. will be printed on the console.
  128.  
  129. ### Question 8
  130. ```js
  131. const isIterable = (object) => {
  132. if (object == null) return false;
  133.  
  134. return typeof object[Symbol.iterator] === 'function';
  135. }
  136. ```
Add Comment
Please, Sign In to add comment