Guest User

Untitled

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