Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="description" content="validateKeys_drill">
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width">
  7. <title>JS Bin</title>
  8. </head>
  9. <body>
  10.  
  11. <script id="jsbin-javascript">
  12. var objectA = {
  13. id: 2,
  14. name: 'Jane Doe',
  15. age: 34,
  16. city: 'Chicago'
  17. }
  18.  
  19. var objectB = {
  20. id: 3,
  21. age: 33,
  22. city: 'Peoria'
  23. }
  24.  
  25. var objectC = {
  26. id: 9,
  27. name: 'Billy Bear',
  28. age: 62,
  29. city: 'Milwaukee',
  30. status: 'paused'
  31. }
  32.  
  33.  
  34. var expectedKeys = [
  35. 'id', 'name', 'age', 'city'
  36. ];
  37.  
  38. function validateKeys(object, expectedKeys) {
  39. if (Object.keys(object).length !== expectedKeys.length) {
  40. return false;
  41. }
  42.  
  43.  
  44. for (var i; i<expectedKeys.length; i++) {
  45. if (!Object.keys(object).find(function(key) {
  46. return key === expectedKeys[i];
  47. })) {
  48. return false;
  49. }
  50. }
  51.  
  52.  
  53. //(.map way that i was working on)
  54. // var keys = Object.keys(object);
  55. // var exists = keys.find()
  56. // for(var k = 0; k<keys.length; k++) {
  57. // keys[k]
  58.  
  59. // }
  60.  
  61.  
  62.  
  63.  
  64. //*from here down is test
  65.  
  66. return true;
  67. }
  68.  
  69. function testIt() {
  70. var objectA = {
  71. id: 2,
  72. name: 'Jane Doe',
  73. age: 34,
  74. city: 'Chicago'
  75. }
  76.  
  77. var objectB = {
  78. id: 3,
  79. age: 33,
  80. city: 'Peoria'
  81. }
  82.  
  83. var objectC = {
  84. id: 9,
  85. name: 'Billy Bear',
  86. age: 62,
  87. city: 'Milwaukee',
  88. status: 'paused'
  89. }
  90.  
  91. var expectedKeys = [
  92. 'id', 'name', 'age', 'city'
  93. ];
  94.  
  95. if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') {
  96. console.error(
  97. 'FAILURE: `validateKeys` should return a boolean value');
  98. return;
  99. }
  100.  
  101.  
  102. if (!validateKeys(objectA, expectedKeys)) {
  103. console.error(
  104. 'FAILURE: running `validateKeys` with the following object and keys ' +
  105. 'should return `true` but returned `false`:\n' +
  106. objectA + '\n' + expectedKeys
  107. )
  108. return;
  109. }
  110.  
  111. if (validateKeys(objectB, expectedKeys)) {
  112. console.error(
  113. 'FAILURE: running `validateKeys` with the following object and keys ' +
  114. 'should return `false` but returned `true`:\n' +
  115. objectB + '\n' + expectedKeys
  116. );
  117. }
  118.  
  119. if (validateKeys(objectC, expectedKeys)) {
  120. console.error(
  121. 'FAILURE: running `validateKeys` with the following object and keys ' +
  122. 'should return `false` but returned `true`:\n' +
  123. objectC + '\n' + expectedKeys
  124. );
  125. }
  126.  
  127. console.log('SUCCESS: `validateKeys` is working');
  128. }
  129.  
  130. testIt()
  131. </script>
  132.  
  133.  
  134.  
  135. <script id="jsbin-source-javascript" type="text/javascript">var objectA = {
  136. id: 2,
  137. name: 'Jane Doe',
  138. age: 34,
  139. city: 'Chicago'
  140. }
  141.  
  142. var objectB = {
  143. id: 3,
  144. age: 33,
  145. city: 'Peoria'
  146. }
  147.  
  148. var objectC = {
  149. id: 9,
  150. name: 'Billy Bear',
  151. age: 62,
  152. city: 'Milwaukee',
  153. status: 'paused'
  154. }
  155.  
  156.  
  157. var expectedKeys = [
  158. 'id', 'name', 'age', 'city'
  159. ];
  160.  
  161. function validateKeys(object, expectedKeys) {
  162. if (Object.keys(object).length !== expectedKeys.length) {
  163. return false;
  164. }
  165.  
  166.  
  167. for (var i; i<expectedKeys.length; i++) {
  168. if (!Object.keys(object).find(function(key) {
  169. return key === expectedKeys[i];
  170. })) {
  171. return false;
  172. }
  173. }
  174.  
  175.  
  176. //(.map way that i was working on)
  177. // var keys = Object.keys(object);
  178. // var exists = keys.find()
  179. // for(var k = 0; k<keys.length; k++) {
  180. // keys[k]
  181.  
  182. // }
  183.  
  184.  
  185.  
  186.  
  187. //*from here down is test
  188.  
  189. return true;
  190. }
  191.  
  192. function testIt() {
  193. var objectA = {
  194. id: 2,
  195. name: 'Jane Doe',
  196. age: 34,
  197. city: 'Chicago'
  198. }
  199.  
  200. var objectB = {
  201. id: 3,
  202. age: 33,
  203. city: 'Peoria'
  204. }
  205.  
  206. var objectC = {
  207. id: 9,
  208. name: 'Billy Bear',
  209. age: 62,
  210. city: 'Milwaukee',
  211. status: 'paused'
  212. }
  213.  
  214. var expectedKeys = [
  215. 'id', 'name', 'age', 'city'
  216. ];
  217.  
  218. if (typeof validateKeys(objectA, expectedKeys) !== 'boolean') {
  219. console.error(
  220. 'FAILURE: `validateKeys` should return a boolean value');
  221. return;
  222. }
  223.  
  224.  
  225. if (!validateKeys(objectA, expectedKeys)) {
  226. console.error(
  227. 'FAILURE: running `validateKeys` with the following object and keys ' +
  228. 'should return `true` but returned `false`:\n' +
  229. objectA + '\n' + expectedKeys
  230. )
  231. return;
  232. }
  233.  
  234. if (validateKeys(objectB, expectedKeys)) {
  235. console.error(
  236. 'FAILURE: running `validateKeys` with the following object and keys ' +
  237. 'should return `false` but returned `true`:\n' +
  238. objectB + '\n' + expectedKeys
  239. );
  240. }
  241.  
  242. if (validateKeys(objectC, expectedKeys)) {
  243. console.error(
  244. 'FAILURE: running `validateKeys` with the following object and keys ' +
  245. 'should return `false` but returned `true`:\n' +
  246. objectC + '\n' + expectedKeys
  247. );
  248. }
  249.  
  250. console.log('SUCCESS: `validateKeys` is working');
  251. }
  252.  
  253. testIt()</script></body>
  254. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement