Guest User

Untitled

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