Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. // JS Part 3 Section 5 Challenge 3 // cows.js
  12.  
  13. var canyonCows = [
  14. {name: "Bessie", type: "cow", hadCalf: "Burt"},
  15. {name: "Donald", type: "bull", hadCalf: null},
  16. {name: "Esther", type: "calf", hadCalf: null},
  17. {name: "Burt", type: "calf", hadCalf: null},
  18. {name: "Sarah", type: "cow", hadCalf: "Esther"},
  19. {name: "Samson", type: "bull", hadCalf: null},
  20. {name: "Delilah", type: "cow", hadCalf: null}
  21. ];
  22. var valleyCows = [
  23. {name: "Danielle", type: "cow", hadCalf: null},
  24. {name: "Brittany", type: "cow", hadCalf: "Christina"},
  25. {name: "Jordan", type: "bull", hadCalf: null},
  26. {name: "Trevor", type: "bull", hadCalf: null},
  27. {name: "Christina", type: "calf", hadCalf: null},
  28. {name: "Lucas", type: "bull", hadCalf: null}
  29. ];
  30. var forestCows = [
  31. {name: "Legolas", type: "calf", hadCalf: null},
  32. {name: "Gimli", type: "bull", hadCalf: null},
  33. {name: "Arwen", type: "cow", hadCalf: null},
  34. {name: "Galadriel", type: "cow", hadCalf: null},
  35. {name: "Eowyn", type: "cow", hadCalf: "Legolas"}
  36. ];
  37.  
  38. Array.prototype.countCattle = function(kind) {
  39. var numKind = 0;
  40. for ( var i = 0; i < this.length; i++) {
  41. if (this[i].type == kind) { // IF THIS[ARRAY INDEX "i"].TYPE is equal to == kind then..
  42. numKind += 1; // += (add 1 to...) numKind
  43. }
  44. }
  45. return numKind;
  46. };
  47.  
  48. alert( canyonCows.countCattle("calf") + "\n" + valleyCows.countCattle("bull") + "\n" + forestCows.countCattle("cow") );
  49. </script>
  50.  
  51.  
  52.  
  53. <script id="jsbin-source-javascript" type="text/javascript">// JS Part 3 Section 5 Challenge 3 // cows.js
  54.  
  55. var canyonCows = [
  56. {name: "Bessie", type: "cow", hadCalf: "Burt"},
  57. {name: "Donald", type: "bull", hadCalf: null},
  58. {name: "Esther", type: "calf", hadCalf: null},
  59. {name: "Burt", type: "calf", hadCalf: null},
  60. {name: "Sarah", type: "cow", hadCalf: "Esther"},
  61. {name: "Samson", type: "bull", hadCalf: null},
  62. {name: "Delilah", type: "cow", hadCalf: null}
  63. ];
  64. var valleyCows = [
  65. {name: "Danielle", type: "cow", hadCalf: null},
  66. {name: "Brittany", type: "cow", hadCalf: "Christina"},
  67. {name: "Jordan", type: "bull", hadCalf: null},
  68. {name: "Trevor", type: "bull", hadCalf: null},
  69. {name: "Christina", type: "calf", hadCalf: null},
  70. {name: "Lucas", type: "bull", hadCalf: null}
  71. ];
  72. var forestCows = [
  73. {name: "Legolas", type: "calf", hadCalf: null},
  74. {name: "Gimli", type: "bull", hadCalf: null},
  75. {name: "Arwen", type: "cow", hadCalf: null},
  76. {name: "Galadriel", type: "cow", hadCalf: null},
  77. {name: "Eowyn", type: "cow", hadCalf: "Legolas"}
  78. ];
  79.  
  80. Array.prototype.countCattle = function(kind) {
  81. var numKind = 0;
  82. for ( var i = 0; i < this.length; i++) {
  83. if (this[i].type == kind) { // IF THIS[ARRAY INDEX "i"].TYPE is equal to == kind then..
  84. numKind += 1; // += (add 1 to...) numKind
  85. }
  86. }
  87. return numKind;
  88. };
  89.  
  90. alert( canyonCows.countCattle("calf") + "\n" + valleyCows.countCattle("bull") + "\n" + forestCows.countCattle("cow") );
  91.  
  92. </script></body>
  93. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement