Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // JS Part 3 Section 5 Challenge 3 // cows.js
- var canyonCows = [
- {name: "Bessie", type: "cow", hadCalf: "Burt"},
- {name: "Donald", type: "bull", hadCalf: null},
- {name: "Esther", type: "calf", hadCalf: null},
- {name: "Burt", type: "calf", hadCalf: null},
- {name: "Sarah", type: "cow", hadCalf: "Esther"},
- {name: "Samson", type: "bull", hadCalf: null},
- {name: "Delilah", type: "cow", hadCalf: null}
- ];
- var valleyCows = [
- {name: "Danielle", type: "cow", hadCalf: null},
- {name: "Brittany", type: "cow", hadCalf: "Christina"},
- {name: "Jordan", type: "bull", hadCalf: null},
- {name: "Trevor", type: "bull", hadCalf: null},
- {name: "Christina", type: "calf", hadCalf: null},
- {name: "Lucas", type: "bull", hadCalf: null}
- ];
- var forestCows = [
- {name: "Legolas", type: "calf", hadCalf: null},
- {name: "Gimli", type: "bull", hadCalf: null},
- {name: "Arwen", type: "cow", hadCalf: null},
- {name: "Galadriel", type: "cow", hadCalf: null},
- {name: "Eowyn", type: "cow", hadCalf: "Legolas"}
- ];
- Array.prototype.countCattle = function(kind) {
- var numKind = 0;
- for ( var i = 0; i < this.length; i++) {
- if (this[i].type == kind) { // IF THIS[ARRAY INDEX "i"].TYPE is equal to == kind then..
- numKind += 1; // += (add 1 to...) numKind
- }
- }
- return numKind;
- };
- alert( canyonCows.countCattle("calf") + "\n" + valleyCows.countCattle("bull") + "\n" + forestCows.countCattle("cow") );
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// JS Part 3 Section 5 Challenge 3 // cows.js
- var canyonCows = [
- {name: "Bessie", type: "cow", hadCalf: "Burt"},
- {name: "Donald", type: "bull", hadCalf: null},
- {name: "Esther", type: "calf", hadCalf: null},
- {name: "Burt", type: "calf", hadCalf: null},
- {name: "Sarah", type: "cow", hadCalf: "Esther"},
- {name: "Samson", type: "bull", hadCalf: null},
- {name: "Delilah", type: "cow", hadCalf: null}
- ];
- var valleyCows = [
- {name: "Danielle", type: "cow", hadCalf: null},
- {name: "Brittany", type: "cow", hadCalf: "Christina"},
- {name: "Jordan", type: "bull", hadCalf: null},
- {name: "Trevor", type: "bull", hadCalf: null},
- {name: "Christina", type: "calf", hadCalf: null},
- {name: "Lucas", type: "bull", hadCalf: null}
- ];
- var forestCows = [
- {name: "Legolas", type: "calf", hadCalf: null},
- {name: "Gimli", type: "bull", hadCalf: null},
- {name: "Arwen", type: "cow", hadCalf: null},
- {name: "Galadriel", type: "cow", hadCalf: null},
- {name: "Eowyn", type: "cow", hadCalf: "Legolas"}
- ];
- Array.prototype.countCattle = function(kind) {
- var numKind = 0;
- for ( var i = 0; i < this.length; i++) {
- if (this[i].type == kind) { // IF THIS[ARRAY INDEX "i"].TYPE is equal to == kind then..
- numKind += 1; // += (add 1 to...) numKind
- }
- }
- return numKind;
- };
- alert( canyonCows.countCattle("calf") + "\n" + valleyCows.countCattle("bull") + "\n" + forestCows.countCattle("cow") );
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement