Advertisement
Guest User

Untitled

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