Guest User

Untitled

a guest
Dec 15th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. //=========================
  2. // OBJECT CREATOR |
  3. //=========================
  4.  
  5. function createMyObject() {
  6. // your code here
  7. return myObject = {
  8. foo: 'bar',
  9. answerToUniverse: 42,
  10. 'olly olly': 'oxen free',
  11. sayHello: function(){
  12. return 'hello';
  13. }
  14. }
  15. }
  16.  
  17. /* From here down, you are not expected to
  18. understand.... for now :)
  19.  
  20.  
  21. Nothing to see here!
  22.  
  23. */
  24.  
  25. (function testCreateMyObject() {
  26. var obj = createMyObject();
  27. if (typeof obj !== 'object') {
  28. console.error('ERROR: `createMyObject` must return an object');
  29. return false;
  30. }
  31. var expectedKeys = ['foo', 'answerToUniverse', 'olly olly', 'sayHello'];
  32. expectedKeys.forEach(function(key) {
  33. if (!(key in obj)) {
  34. console.error('ERROR: Missing a key for ' + key);
  35. return false;
  36. }
  37. });
  38. if (obj.foo !== 'bar') {
  39. console.error("ERROR: Value for `foo` should be 'bar' but was " + obj.foo);
  40. return false;
  41. }
  42. if (obj.answerToUniverse !== 42) {
  43. console.error(
  44. 'ERROR: Value for `answerToUniverse` should be 42 but was ' +
  45. obj.answerToUniverse
  46. );
  47. return false;
  48. }
  49. if (obj['olly olly'] !== 'oxen free') {
  50. console.error(
  51. "ERROR: Value for `'olly olly'` should be 'oxen free' but was " +
  52. obj['olly olly']
  53. );
  54. return false;
  55. }
  56. if (!(typeof obj.sayHello === 'function' && obj.sayHello() === 'hello')) {
  57. console.error(
  58. "ERROR: Value for `sayHello` must be a function that returns the string 'hello'"
  59. );
  60. return false;
  61. }
  62. console.log('SUCCESS: Your function works!');
  63. })();
  64.  
  65.  
  66.  
  67.  
  68.  
  69. //==================================================================
  70.  
  71. //=========================
  72. // OBJECT UPDATER |
  73. //=========================
  74.  
  75. function updateObject(obj) {
  76. obj.foo = 'foo';
  77. obj.bar = 'bar';
  78. obj.bizz = 'bizz';
  79. obj.bang = 'bang';
  80.  
  81. return obj;
  82. }
  83.  
  84.  
  85.  
  86. /* From here down, you are not expected to
  87. understand.... for now :)
  88.  
  89.  
  90. Nothing to see here!
  91.  
  92. */
  93.  
  94. (function testUpdateObject() {
  95. var oldObj = {
  96. cats: 'cats',
  97. dogs: 'dogs',
  98. };
  99. var newObj = updateObject(oldObj);
  100. if (typeof newObj !== 'object') {
  101. console.error('ERROR: `createMyObject` must return an object');
  102. return false
  103. }
  104. ['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
  105. if (!(key in newObj)) {
  106. console.error('ERROR: `' + key + '` not in object returned by `updateObject`');
  107. return false;
  108. }
  109. });
  110. ['foo', 'bar', 'bizz', 'bang'].forEach(function(key) {
  111. if (newObj[key] !== key) {
  112. console.error('ERROR: `' + key + '` should be "' + key + '" but was ' + newObj[key]);
  113. return false;
  114. }
  115. });
  116. if (!(newObj.cats === 'cats' && newObj.dogs === 'dogs')) {
  117. console.error('ERROR: your function doesn\'t preserve existing key/value pairs');
  118. return false;
  119. }
  120. console.log('SUCCESS: `updateObject` works correctly!');
  121.  
  122. })();
  123.  
  124.  
  125.  
  126.  
  127. //==================================================================
  128.  
  129. //=========================
  130. // SELF-REFERENCE |
  131. //=========================
  132.  
  133. function personMaker() {
  134. var person = {
  135. firstName: 'Paul',
  136. lastName: 'Jones',
  137.  
  138. fullName: function(){
  139. return `${this.firstName} ${this.lastName}`;
  140. },
  141. };
  142. return person;
  143. }
  144.  
  145. /* From here down, you are not expected to
  146. understand.... for now :)
  147.  
  148.  
  149. Nothing to see here!
  150.  
  151. */
  152.  
  153. (function testPersonMaker() {
  154. var person = personMaker();
  155. if (typeof person !== 'object') {
  156. console.error('ERROR: `personMaker` must return an object');
  157. return false;
  158. }
  159. if (typeof person.fullName !== 'function') {
  160. console.error('ERROR: `fullName` must be a method');
  161. return false;
  162. }
  163. if (person.fullName() !== 'Paul Jones') {
  164. console.error(
  165. 'ERROR: The value for `fullName` should be "Paul Jones" but was ' +
  166. person.fullName()
  167. );
  168. return false;
  169. }
  170. person.firstName = 'Lisa';
  171. person.lastName = 'Simpson';
  172. if (person.fullName() !== 'Lisa Simpson') {
  173. console.error(
  174. '`personMaker` is not using self reference correctly. ' +
  175. 'When firstName set to "Lisa" and lastName set to "Simpson", ' +
  176. 'should return "Lisa Simpson" but returned ' +
  177. person.fullName()
  178. );
  179. }
  180. console.log('SUCCESS: `updateObject` works correctly!');
  181. })();
  182.  
  183.  
  184.  
  185.  
  186.  
  187. //==================================================================
  188.  
  189. //=========================
  190. // DELETING KEYS |
  191. //=========================
  192.  
  193. const sampleObj = {
  194. foo: 'foo',
  195. bar: 'bar',
  196. bizz: 'bizz',
  197. bang: 'bang',
  198. };
  199.  
  200. function keyDeleter(obj) {
  201. delete sampleObj.foo;
  202. delete sampleObj.bar;
  203.  
  204. return sampleObj;
  205. }
  206.  
  207. /* From here down, you are not expected to
  208. understand.... for now :)
  209.  
  210.  
  211. Nothing to see here!
  212.  
  213. */
  214.  
  215. (function testKeyDeleter() {
  216. var obj = keyDeleter({
  217. foo: 'foo',
  218. bar: 'bar',
  219. bizz: 'bizz',
  220. bang: 'bang',
  221. });
  222.  
  223. if (typeof obj !== 'object') {
  224. console.error('ERROR: `keyDeleter` must be return an object');
  225. return false;
  226. }
  227. ['foo', 'bar'].forEach(function(key) {
  228. if (key in obj) {
  229. console.error('`keyDeleter` did not delete the key for ' + key);
  230. return false;
  231. }
  232. });
  233. ['bizz', 'bang'].forEach(function(key) {
  234. if (!(key in obj && obj[key] === key)) {
  235. console.error('`keyDeleter` is deleting keys other than `foo` and `bar`');
  236. return false;
  237. }
  238. });
  239. console.log('SUCCESS: `keyDeleter` works correctly!');
  240. })();
Add Comment
Please, Sign In to add comment