Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. function createMyObject() {
  2. return {
  3. foo: 'bar',
  4. answerToUniverse: 42,
  5. 'olly olly': 'oxen free',
  6. sayHello: function() {
  7. return 'hello';
  8. }
  9. };
  10. }
  11.  
  12.  
  13.  
  14. /* From here down, you are not expected to
  15. understand.... for now :)
  16.  
  17.  
  18. Nothing to see here!
  19.  
  20. */
  21.  
  22. (function testCreateMyObject() {
  23. var obj = createMyObject();
  24. if (typeof obj !== 'object') {
  25. console.error('ERROR: `createMyObject` must return an object');
  26. return false
  27. }
  28. var expectedKeys = ['foo', 'answerToUniverse', 'olly olly', 'sayHello'];
  29. expectedKeys.forEach(function(key) {
  30. if (!(key in obj)) {
  31. console.error('ERROR: Missing a key for ' + key);
  32. return false;
  33. }
  34. });
  35. if (obj.foo !== 'bar') {
  36. console.error('ERROR: Value for `foo` should be \'bar\' but was ' + obj.foo);
  37. return false;
  38. }
  39. if (obj.answerToUniverse !== 42) {
  40. console.error(
  41. 'ERROR: Value for `answerToUniverse` should be 42 but was ' + obj.answerToUniverse);
  42. return false;
  43. }
  44. if (obj['olly olly'] !== 'oxen free') {
  45. console.error(
  46. 'ERROR: Value for `\'olly olly\'` should be \'oxen free\' but was ' + obj['olly olly']);
  47. return false;
  48. }
  49. if (!(typeof obj.sayHello === "function" && obj.sayHello() === 'hello')) {
  50. console.error('ERROR: Value for `sayHello` must be a function that returns the string \'hello\'');
  51. return false;
  52. }
  53. console.log('SUCCESS: Your function works!');
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement