Guest User

Untitled

a guest
Apr 24th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. // ================================================================================
  2. // You will be writing a series of functions beneath each block of comments below.
  3. // Tackle each function one at a time. You may move on to a new exercise and return
  4. // later. Run the code and the console to the right will tell you if your function
  5. // is successfully passing the tests.
  6. // ================================================================================
  7.  
  8.  
  9. /*
  10. Define a function named `kmToMiles` that receives one parameter:
  11.  
  12. 1) km - type: number
  13. The function should return a number in miles, rounded down to nearest integer.
  14.  
  15. HINT: There are 1.6km in 1 mile.
  16. HINT: What method on Javascript's Math object can round decimals down to nearest whole number?
  17. */
  18. function kmToMiles(number) {
  19. return Math.round(number / 1.6)
  20. }
  21.  
  22. /*
  23. Define a function named `countLetters` that receives two parameters:
  24. 1) sentence - type: string
  25. 2) character - type: string
  26. The function should return a count of every `character` present in `sentence`.
  27.  
  28. HINT: You can access a single character in a string just like accessing an element in an array - `myString[3]` would access the fourth character
  29. */
  30. function countLetters(sentence, character) {
  31. for (let i = 0; i < sentence.length, i++) {
  32. return
  33. }
  34. }
  35. /*
  36. Define a function named `middleElement` that receives one parameter:
  37. 1) items - type: array
  38. The function should return the element present in the middle index. If
  39. the array contains an even number of elements, return the item in the
  40. first half of the array closest to the middle.
  41.  
  42. HINT: You can use the expression `n % 2 === 0` to check if `n` is an even number
  43. */
  44. function middleElement(items) {
  45. return items[items.length /2]
  46. }
  47. /*
  48. Define a function named `rightToVote` that receives one parameter:
  49. 1) person - type: object
  50. The function should check the `age` property on `person`. If it is 18 or older,
  51. return true. Otherwise, return false. If `age` is not a number, throw an error.
  52.  
  53. HINT: The `typeof` operator can tell you what data type a variable is
  54. */
  55. function rightToVote(person) {
  56. if(person.age >= 18) {
  57. return true
  58. }
  59. else if (person.age <18){
  60. return false
  61. }
  62. else {
  63. throw "Parameter is not a number" }
  64. }
  65. /*
  66. Define a factory function `createStudent` that receives two parameters:
  67. 1) name - type: string
  68. 2) location - type: string
  69. This factory should return an object that includes the provided `name` and `location`
  70. properties and also has a `describe` method that returns the string: "Hello, I
  71. am {name} and I live in {location}."
  72.  
  73. HINT: You will want to use the `this` keyword
  74. */
  75. function createStudent(name, location) {
  76. const student = {
  77. name : name,
  78. location : location
  79. }
  80. return
  81. //`Hello, I am ${this.name} and I live in ${this.location}`
  82.  
  83. }
  84.  
  85.  
  86.  
  87. /*
  88. ===========================================
  89. IGNORE ALL CODE BELOW HERE
  90. ===========================================
  91. |
  92. |
  93. |
  94. |
  95. |
  96. |
  97. |
  98. |
  99. */
  100. /* eslint-disable no-console */
  101. (function(){
  102. function fnExists(fnName) {
  103. return window[fnName] !== undefined;
  104. }
  105.  
  106. function testKmToMiles() {
  107. const FN_NAME = 'kmToMiles';
  108. if (!fnExists(FN_NAME))
  109. return console.log(`\`${FN_NAME}\` - FAIL: function is not defined`);
  110.  
  111. const results = [];
  112. const testArgs = [50, 79];
  113. const expected = [31, 49];
  114.  
  115. try {
  116. testArgs.forEach(ta => results.push(window[FN_NAME](ta)));
  117. } catch(e) {
  118. return console.log(`\`${FN_NAME}\` - FAIL: function produced unexpected error: ${e.message}`);
  119. }
  120.  
  121. for (let i = 0; i < results.length; i++) {
  122. if (results[i] !== expected[i]) {
  123. return console.log(`\`${FN_NAME}(${testArgs[i]})\` - FAIL: expected ${expected[i]}, got ${results[i]}`);
  124. }
  125. }
  126.  
  127. console.log(`\`${FN_NAME}\` - SUCCESS: All tests passed`);
  128. }
  129.  
  130. function testCountLetters() {
  131. const FN_NAME = 'countLetters';
  132. if (!fnExists(FN_NAME))
  133. return console.log(`\`${FN_NAME}\` - FAIL: function is not defined`);
  134.  
  135. const results = [];
  136. const testArgs = [
  137. ['fly me to the moon', 'o'],
  138. ['do or do not. there is no try', 'e']
  139. ];
  140. const expected = [3, 2];
  141.  
  142. try {
  143. testArgs.forEach(ta => results.push(window[FN_NAME](...ta)));
  144. } catch(e) {
  145. return console.log(`\`${FN_NAME}\` - FAIL: function produced unexpected error: ${e.message}`);
  146. }
  147.  
  148. for (let i = 0; i < results.length; i++) {
  149. if (results[i] !== expected[i]) {
  150. return console.log(`\`${FN_NAME}(${testArgs[i].map(e => `'${e}'`).join(', ')})\` - FAIL: expected ${expected[i]}, got ${results[i]}`);
  151. }
  152. }
  153.  
  154. console.log(`\`${FN_NAME}\` - SUCCESS: All tests passed`);
  155. }
  156.  
  157. function testMiddleElement() {
  158. const FN_NAME = 'middleElement';
  159. if (!fnExists(FN_NAME))
  160. return console.log(`\`${FN_NAME}\` - FAIL: function is not defined`);
  161.  
  162. const results = [];
  163. const testArgs = [
  164. [2,5,7,10,34],
  165. [2,5,10,34]
  166. ];
  167. const expected = [7, 5];
  168.  
  169. try {
  170. testArgs.forEach(ta => results.push(window[FN_NAME](ta)));
  171. } catch(e) {
  172. return console.log(`\`${FN_NAME}\` - FAIL: function produced unexpected error: ${e.message}`);
  173. }
  174.  
  175. for (let i = 0; i < results.length; i++) {
  176. if (results[i] !== expected[i]) {
  177. return console.log(`\`${FN_NAME}([${testArgs[i]}])\` - FAIL: expected ${expected[i]}, got ${results[i]}`);
  178. }
  179. }
  180.  
  181. console.log(`\`${FN_NAME}\` - SUCCESS: All tests passed`);
  182. }
  183.  
  184. function testRightToVote() {
  185. const FN_NAME = 'rightToVote';
  186. const fn = window[FN_NAME];
  187. if (!fnExists(FN_NAME))
  188. return console.log(`\`${FN_NAME}\` - FAIL: function is not defined`);
  189.  
  190. const results = [];
  191. const testArgs = [
  192. { name: 'rich', age: 25 },
  193. { name: 'rich', age: 15 },
  194. { name: 'john', age: 18 }
  195. ];
  196. const expected = [true, false, true];
  197.  
  198. try {
  199. testArgs.forEach(ta => results.push(window[FN_NAME](ta)));
  200. } catch(e) {
  201. return console.log(`\`${FN_NAME}\` - FAIL: function produced unexpected error: ${e.message}`);
  202. }
  203.  
  204. for (let i = 0; i < results.length; i++) {
  205. if (results[i] !== expected[i]) {
  206. return console.log(`\`${FN_NAME}(${JSON.stringify(testArgs[i])})\` - FAIL: expected ${expected[i]}, got ${results[i]}`);
  207. }
  208. }
  209.  
  210. // Custom tests
  211. let result1, result2;
  212. try {
  213. result1 = fn({ name: 'rich', age: undefined });
  214. return console.log(`\`${FN_NAME}\` - FAIL: expected error to be thrown when 'age' not a number`);
  215. } catch(e) {
  216. // Successful test if error thrown... caveat: not testing for specific error
  217. }
  218.  
  219. console.log(`\`${FN_NAME}\` - SUCCESS: All tests passed`);
  220. }
  221.  
  222. function testCreateStudent() {
  223. const FN_NAME = 'createStudent';
  224. const fn = window[FN_NAME];
  225. if (!fnExists(FN_NAME))
  226. return console.log(`\`${FN_NAME}\` - FAIL: function is not defined`);
  227.  
  228. let result1;
  229. try {
  230. result1 = fn('Rich', 'Los Angeles');
  231. } catch(e) {
  232. return console.log(`\`${FN_NAME}\` - FAIL: function produced unexpected error: ${e.message}`);
  233. }
  234.  
  235. let failMsg = (msg) => `\`${FN_NAME}('Rich', 'Los Angeles')\` - FAIL: ${msg}`;
  236.  
  237. if (typeof result1 !== 'object')
  238. return console.log(failMsg('did not return an object'));
  239. if (result1.name !== 'Rich')
  240. return console.log(failMsg(`expected prop 'name' to be 'Rich', instead got '${result1.name}'`));
  241. if (result1.location !== 'Los Angeles')
  242. return console.log(failMsg(`expected prop 'location' to be 'Los Angeles', instead got '${result1.location}'`));
  243. if (typeof result1.describe !== 'function')
  244. return console.log(failMsg('expected prop \'describe\' to be a function'));
  245. if (result1.describe() !== 'Hello, I am Rich and I live in Los Angeles.')
  246. return console.log(failMsg('\'describe\' method did not return correct message'));
  247.  
  248. result1.name = 'Joe';
  249. if (result1.describe() !== 'Hello, I am Joe and I live in Los Angeles.')
  250. return console.log(failMsg('\'describe\' method did not return correct message. Are you using `this` correctly?'));
  251.  
  252. console.log(`\`${FN_NAME}\` - SUCCESS: All tests passed`);
  253. }
  254.  
  255.  
  256. testKmToMiles();
  257. testCountLetters();
  258. testMiddleElement();
  259. testRightToVote();
  260. testCreateStudent();
  261. }());
Add Comment
Please, Sign In to add comment