Advertisement
Guest User

Untitled

a guest
May 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. ### Question 1
  2. ##### Cause of Error
  3. Since the if statement evalutes to a 0 if the word superman is the first word, this evaluates to false and the not operator
  4. evalutes it to a true, hence the error is thrown.
  5.  
  6. ##### Solution
  7. Adding a 1 to the 0 will eliminate the if statement evaluating a 0 and thus the sentence 'Superman is awesome!' does not give an error.Regardless of the index for the word superman. Another solution is to comparison operator with a negative value.
  8.  
  9. ##### Code
  10. function validateString(str) {
  11. if (!(str.toLowerCase().indexOf('superman') + 1)) {
  12. throw new Error('String does not contain superman');
  13. }
  14.  
  15. }
  16.  
  17. function validateString(str) {
  18. if (!(str.toLowerCase().indexOf('superman') !== -1)) {
  19. throw new Error('String does not contain superman');
  20. }
  21.  
  22. }
  23.  
  24.  
  25. ### Question 2
  26. ##### Assumptions
  27. 1. Array is sorted completely
  28. 2. Array comprises of only integers e.g. [0,1,2,3,4]
  29.  
  30. ##### Solution
  31. What we can use here is a simple binary search that returns a true or false depending on the presence of the demanded integer in the array.
  32. Binary Search divides up the array in to 2 halfs to look for integer 'x'. if 'x' is in the lower half we
  33. reduce the upper bound (and viceversa) using the middle index dependant on start and end values for the algorithm to sweep through the appropriate part of the array to check for integer 'x'.
  34. This is a recursive function that either increases or decreases the start/end indices depending on the location of 'x' in the array with a time complaxity of
  35. O(lgn).
  36.  
  37. ##### Code
  38. function binarySearch (arr, x, l, r) {
  39.  
  40. // Base Condtion
  41. if (l > r) return false;
  42.  
  43. // Find the middle index
  44. let mid = Math.floor((l + r)/2);
  45.  
  46. // Compare mid with given key x
  47. if (arr[mid]===x) return true;
  48.  
  49. // If element at mid is greater than x,
  50. // search in the left half of mid
  51. if(arr[mid] > x)
  52. return binarySearch(arr, x, l, mid-1);
  53. else
  54.  
  55. // If element at mid is smaller than x,
  56. // search in the right half of mid
  57. return binarySearch(arr, x, mid+1, r);
  58. }
  59.  
  60.  
  61. ### Question 3
  62. function fixPhoneNumber(value, delimiter = '-') {
  63. let number = '';
  64. value = value.trim();
  65. for (el of value) {
  66. if (!isNaN(el) && el !== ' ') number += el;
  67. }
  68. if (number.length !== 10 || !number) {
  69. throw Error('Number does not have sufficient disgits');
  70. }
  71. return `${number.substr(0,3)}${delimiter}${number.substr(3,3)}${delimiter}${number.substr(6)}`
  72. }
  73.  
  74. /*Phone Numbers with extensions will not pass this function or numbers that
  75. do not follow US phone number formats*/
  76.  
  77. ### Question 4
  78. const assert = require('assert')
  79. const fizzBuzz = require('./fizBuzz');
  80.  
  81. describe('fizzBuzz', function() {
  82. it('should render an error with invalid start and stop values', function() {
  83. const start = 1;
  84. const stop = 0
  85. assert.throws(() => fizzBuzz(start, stop), Error, 'Invalid arguments')
  86. })
  87. it('should return 1 for equal stop and start', function() {
  88. const start = 1;
  89. const stop = 1;
  90. const result = fizzBuzz(start, stop);
  91. assert.equal('1', result);
  92. });
  93. it('should return valid result with stop value of 2', function() {
  94. const start = 1;
  95. const stop = 2;
  96. const res = fizzBuzz(start, stop);
  97. assert.equal('2', res[1]);
  98. });
  99. it('should return valid result with stop value of 4', function() {
  100. const start = 1;
  101. const stop = 4;
  102. const res = fizzBuzz(start, stop);
  103. assert.equal('12Fizz4', res)
  104. })
  105. it('should return valid result with stop value of 7', function() {
  106. const start = 1;
  107. const stop = 7;
  108. const res = fizzBuzz(start, stop);
  109. assert.equal('12Fizz4BuzzFizz7', res)
  110. })
  111. it('should return valid result with stop value of 10', function() {
  112. const start = 1;
  113. const stop = 10;
  114. const res = fizzBuzz(start, stop);
  115. assert.equal('12Fizz4BuzzFizz78FizzBuzz', res)
  116. })
  117. it('should return valid result with stop value of 15', function() {
  118. const start = 1;
  119. const stop = 15;
  120. const res = fizzBuzz(start, stop);
  121. assert.equal('12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz', res)
  122. })
  123. it('should return valid result with stop value of 20', function() {
  124. const start = 1;
  125. const stop = 20;
  126. const res = fizzBuzz(start, stop);
  127. assert.equal('12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19Buzz', res)
  128. })
  129. it('should return valid result with stop value of 200 by giving a count value', function() {
  130. const start = 1;
  131. const stop = 200;
  132. const res = fizzBuzz(start, stop).length;
  133. assert.equal(687, res)
  134. })
  135. it('should return valid result with default params', function() {
  136. const res = fizzBuzz().length;
  137. assert.equal(313, res)
  138. })
  139. it('should return valid result with valid start param', function() {
  140. const start = 10;
  141. const res = fizzBuzz(start, undefined).length;
  142. assert.equal(292, res)
  143. })
  144. it('should return valid result with valid stop param', function() {
  145. const stop = 10;
  146. const res = fizzBuzz(undefined, stop).length;
  147. assert.equal(25, res)
  148. })
  149. })
  150.  
  151. ### Question 5
  152. function filterFiles (files, exlcudeFiles) {
  153. return files.filter(dir => !exlcudeFiles.find(path => path === dir || dir.indexOf(path) === 0))
  154. };
  155.  
  156. ### Question 6
  157. For this question I googled the algorithm as my solution did not render optimal results.
  158.  
  159. function getColor(name) {
  160. let hash = 0;
  161. for (let i = 0; i < name.length; i++) {
  162. hash = name.charCodeAt(i) + ((hash << 5) - hash);
  163. }
  164. let colour = '#';
  165. for (let i = 0; i < 3; i++) {
  166. let value = (hash >> (i * 8)) & 0xff;
  167. colour += ('00' + value.toString(16)).substr(-2);
  168. }
  169. return colour;
  170. }
  171.  
  172. ### Question 7
  173. Since the variable i is initialized to 0, when the buttons are clicked we would get the following:
  174. You clicked button #0
  175. You clicked button #3
  176.  
  177. ### Question 8
  178.  
  179. const isIterable = (obj) => {
  180. try {
  181. return typeof obj[Symbol.iterator] === 'function'
  182. }catch(e){
  183. return false
  184. }
  185. }
  186.  
  187. I googled this one. My solution was inoptimal as I used for loops and if statement to check what comes in e.g. array, onject etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement