Guest User

Untitled

a guest
Dec 16th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. function makeStudentsReport(data) {
  2.  
  3. return data.map( d => `${d.name} : ${d.grade}`);
  4.  
  5. }
  6.  
  7. /* From here down, you are not expected to
  8. understand.... for now :)
  9.  
  10. Nothing to see here!
  11.  
  12. */
  13.  
  14. // tests
  15.  
  16. function testIt() {
  17. const testData = [
  18. { name: 'Jane Doe', grade: 'A' },
  19. { name: 'John Dough', grade: 'B' },
  20. { name: 'Jill Do', grade: 'A' },
  21. ];
  22.  
  23. const expectations = ['Jane Doe: A', 'John Dough: B', 'Jill Do: A'];
  24.  
  25. const results = makeStudentsReport(testData);
  26.  
  27. if (!(results && results instanceof Array)) {
  28. console.error('FAILURE: `makeStudentsReport` must return an array');
  29. return;
  30. }
  31. if (results.length !== testData.length) {
  32. console.error(
  33. 'FAILURE: test data had length of ' +
  34. testData.length +
  35. ' but `makeStudentsReport` returned array of length ' +
  36. results.length
  37. );
  38. return;
  39. }
  40. for (let i = 0; i < expectations.length; i++) {
  41. let expect = expectations[i];
  42. if (
  43. !results.find(function(item) {
  44. return item === expect;
  45. })
  46. ) {
  47. console.error(
  48. 'FAILURE: `makeStudentsReport` is not ' + 'producing expected strings'
  49. );
  50. return;
  51. }
  52. }
  53. console.log('SUCCESS: `makeStudentsReport` is working');
  54. }
  55.  
  56. testIt();
Add Comment
Please, Sign In to add comment