Guest User

Untitled

a guest
Nov 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.74 KB | None | 0 0
  1. FIRST -
  2. function makeStudentsReport(data) {
  3. // your code here
  4. function objectValue(obj) {
  5. return obj.name + ': ' + obj.grade;
  6. }
  7. return data.map(objectValue);
  8. }
  9.  
  10.  
  11. /* From here down, you are not expected to
  12. understand.... for now :)
  13.  
  14. Nothing to see here!
  15.  
  16. */
  17.  
  18.  
  19. // tests
  20.  
  21. function testIt() {
  22.  
  23. const testData = [
  24. {name: 'Jane Doe', grade: 'A'},
  25. {name: 'John Dough', grade: 'B'},
  26. {name: 'Jill Do', grade: 'A'}
  27. ];
  28.  
  29. const expectations = [
  30. 'Jane Doe: A',
  31. 'John Dough: B',
  32. 'Jill Do: A'
  33. ];
  34.  
  35. const results = makeStudentsReport(testData);
  36.  
  37. if (!(results && results instanceof Array)) {
  38. console.error(
  39. 'FAILURE: `makeStudentsReport` must return an array');
  40. return
  41. }
  42. if (results.length !== testData.length) {
  43. console.error(
  44. 'FAILURE: test data had length of ' + testData.length +
  45. ' but `makeStudentsReport` returned array of length ' + results.length);
  46. return
  47. }
  48. for (let i=0; i < expectations.length; i++) {
  49. let expect = expectations[i];
  50. if (!results.find(function(item) {
  51. return item === expect;
  52. })) {
  53. console.error(
  54. 'FAILURE: `makeStudentsReport` is not ' +
  55. 'producing expected strings'
  56. );
  57. return
  58. }
  59. }
  60. console.log('SUCCESS: `makeStudentsReport` is working');
  61. }
  62.  
  63. testIt();
  64.  
  65.  
  66. SECOND -
  67. const studentData = [
  68. {
  69. name: 'Tim',
  70. status: 'Current student',
  71. course: 'Biology'
  72. },
  73. {
  74. name: 'Sue',
  75. status: 'Withdrawn',
  76. course: 'Mathematics'
  77. },
  78. {
  79. name: 'Liz',
  80. status: 'On leave',
  81. course: 'Computer science'
  82. }
  83. ];
  84.  
  85. function enrollInSummerSchool(students) {
  86. // your code here
  87. let newArray = students;
  88. for (let i = 0; i < newArray.length; i++) {
  89. newArray[i].status = 'In Summer school';
  90. }
  91. return newArray;
  92. }
  93.  
  94.  
  95. /* From here down, you are not expected to understand.... for now :)
  96. Nothing to see here!
  97.  
  98. */
  99.  
  100. // tests
  101.  
  102.  
  103. function testIt() {
  104. var testData = [
  105. {
  106. name: 'Burt',
  107. status: 'Playing hooky',
  108. course: 'Biology'
  109. },
  110. {
  111. name: 'Melanie',
  112. status: 'Sick',
  113. course: 'Mathematics'
  114. },
  115. {
  116. name: 'Leonard',
  117. status: 'AWOL',
  118. course: 'Computer science'
  119. }
  120. ];
  121.  
  122. var results = enrollInSummerSchool(testData);
  123.  
  124. if (!(results && results instanceof Array)) {
  125. console.error('FAILURE: `enrollSummerSchool` must return an array');
  126. return
  127. }
  128. var result = testData.every(function(student) {
  129. var match = results.find(function(_student) {
  130. return (
  131. _student.name === student.name &&
  132. _student.course === student.course &&
  133. _student.status.toLowerCase() === 'in summer school'
  134. );
  135. }
  136. );
  137. return match !== undefined;
  138. }
  139. );
  140. if (!result) {
  141. console.error(
  142. 'FAILURE: `enrollSummerSchool` should return ' +
  143. 'original key/value pairs for each student, and ' +
  144. 'update `status` to "In Summer school": ' + JSON.stringify(results));
  145. } else {
  146. console.info('SUCCESS: `enrollSummerSchool` is working');
  147. }
  148. }
  149.  
  150. testIt();
  151.  
  152.  
  153. THIRD -
  154. // you can pass in `scratchData` to test out `findByid`
  155. // your function
  156. const scratchData = [
  157. {id: 22, foo: 'bar'},
  158. {id: 28, foo: 'bizz'},
  159. {id: 19, foo: 'bazz'}
  160. ];
  161.  
  162. function findById(items, idNum) {
  163. // your code here
  164. for (let i = 0; i < items.length; i++) {
  165. if (idNum === items[i].id) {
  166. return items[i];
  167. }
  168. }
  169. }
  170.  
  171.  
  172. //
  173.  
  174. function testIt() {
  175. const testData = [
  176. {id: 1, foo: 'bar'},
  177. {id: 2, foo: 'bizz'},
  178. {id: 3, bang: 'boo'}
  179. ];
  180. const result = findById(testData, 3);
  181. if (!(result && result !== null && typeof result === 'object')) {
  182. console.error('`findById` must return an object');
  183. return
  184. }
  185. if (result.id !== 3) {
  186. console.error(
  187. 'Asked for item with id of `3` but got back one with id of ' +
  188. result.id
  189. );
  190. return
  191. }
  192. if (result.bang !== 'boo') {
  193. console.error('Expected all key/value pairs from target object to be returned');
  194. return
  195. }
  196. console.log('SUCCESS: `findByid` is working');
  197. }
  198.  
  199. testIt();
  200.  
  201.  
  202. FOURTH -
  203. // running the function with `objectA` and `expectedKeys`
  204. // should return `true`
  205. const objectA = {
  206. id: 2,
  207. name: 'Jane Doe',
  208. age: 34,
  209. city: 'Chicago'
  210. }
  211.  
  212. // running the function with `objectB` and `expectedKeys`
  213. // should return `false`
  214. const objectB = {
  215. id: 3,
  216. age: 33,
  217. city: 'Peoria'
  218. }
  219.  
  220. const expectedKeys = [
  221. 'id', 'name', 'age', 'city'
  222. ];
  223.  
  224. function validateKeys(object, expectedKeys) {
  225. // your code here
  226. let currentKeys = Object.keys(object);
  227. if (currentKeys.length !== expectedKeys.length) {
  228. return false;
  229. }
  230.  
  231. for (let i = 0; i < expectedKeys.length; i++) {
  232. if (currentKeys[i] !== expectedKeys[i]){
  233. return false;
  234. } else {
  235. return true;
  236. }
  237. }
  238. }
  239. /* From here down, you are not expected to
  240. understand.... for now :)
  241.  
  242.  
  243. Nothing to see here!
  244.  
  245. */
  246.  
  247.  
  248.  
  249. function testIt() {
  250. const objectA = {
  251. id: 2,
  252. name: 'Jane Doe',
  253. age: 34,
  254. city: 'Chicago'
  255. }
  256.  
  257. const objectB = {
  258. id: 3,
  259. age: 33,
  260. city: 'Peoria'
  261. }
  262.  
  263. const objectC = {
  264. id: 9,
  265. name: 'Billy Bear',
  266. age: 62,
  267. city: 'Milwaukee',
  268. status: 'paused'
  269. }
  270.  
  271.  
  272. const objectD = {
  273. foo: 2,
  274. bar: 'Jane Doe',
  275. bizz: 34,
  276. bang: 'Chicago'
  277. }
  278.  
  279. const expectedKeys = [
  280. 'id', 'name', 'age', 'city'
  281. ];
  282.  
  283. if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') {
  284. console.error(
  285. 'FAILURE: validateKeys should return a boolean value');
  286. return;
  287. }
  288.  
  289.  
  290. if (!validateKeys(objectA, expectedKeys)) {
  291. console.error(
  292. `FAILURE: running validateKeys with the following object and keys
  293. should return true but returned false:
  294. Object: ${JSON.stringify(objectA)}
  295. Expected keys: ${expectedKeys}`
  296. )
  297. return;
  298. }
  299.  
  300. if (validateKeys(objectB, expectedKeys)) {
  301. console.error(
  302. `FAILURE: running validateKeys with the following object and keys
  303. should return false but returned true:
  304. Object: ${JSON.stringify(objectB)}
  305. Expected keys: ${expectedKeys}`
  306. )
  307. return;
  308. }
  309.  
  310. if (validateKeys(objectC, expectedKeys)) {
  311. console.error(
  312. `FAILURE: running validateKeys with the following object and keys
  313. should return false but returned true:
  314. Object: ${JSON.stringify(objectC)}
  315. Expected keys: ${expectedKeys}`
  316. )
  317. return;
  318. }
  319.  
  320. if (validateKeys(objectD, expectedKeys)) {
  321. console.error(
  322. `FAILURE: running validateKeys with the following object and keys
  323. should return false but returned true:
  324. Object: ${JSON.stringify(objectD)}
  325. Expected keys: ${expectedKeys}`
  326. )
  327. return;
  328. }
  329.  
  330. console.log('SUCCESS: validateKeys is working');
  331. }
  332.  
  333. testIt()
Add Comment
Please, Sign In to add comment