Guest User

Untitled

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