Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class bunny {
- constructor(name, gender, color, age, radioActive, identified) {
- this.name = name;
- this.gender = gender;
- this.color = color;
- this.age = age;
- this.radioActive = radioActive;
- this.identified = identified;
- }
- }
- // creating variables for the bunny's data
- var bunnyNames = [
- "Sleepy",
- "Flappy",
- "Clurp",
- "Trup",
- "Siuce",
- "Mario",
- "Ash",
- "Wurppy",
- "Tillo",
- "Shivvy"
- ];
- var gender = ["Male", "Female"];
- var color = ["White", "Brown, 'Black", "Spotted"];
- var age;
- var radioActive = false;
- var identified = false;
- var allBunnies = [];
- var maleBunnies = [];
- var femaleBunnies = [];
- var died = 0;
- function createBunny(name, gender, color, age, radioActive, identified) {
- // gets each array and changes the value to a randomized value
- name = name[Math.floor(Math.random() * name.length)];
- gender = gender[Math.floor(Math.random() * gender.length)];
- color = color[Math.floor(Math.random() * color.length)];
- age = 0;
- radioActive = false;
- identified = false;
- if (Math.random() * 10 < 0.02 + 1) radioActive = true;
- // Creates the bunny with the class and puts all value's that were made inside the created bunny, so it gets a different value each time
- var Newbunny = new bunny(name, gender, color, age, radioActive, identified);
- // Pushes the made bunny into an array
- allBunnies.push(Newbunny);
- console.log(name + ` Has been born he's ${age}`);
- for (i = 0; i < maleBunnies.length; i++) {
- console.log("Name " + maleBunnies[i].name + " Age: " + maleBunnies[i].age);
- }
- }
- function updateBunny() {
- // aging each rabbit, so they get older
- for (i = 0; i < allBunnies.length; i++) {
- allBunnies[i].age = allBunnies[i].age + 1;
- // checks if rabbit is over 10 years and makes it get removed
- if (allBunnies[i].age > 9) {
- console.log(allBunnies[i].name + " Has died ");
- allBunnies.splice(0, i);
- died = died + 1;
- }
- // checks each rabbit's gender to put them into different groups
- if (allBunnies[i].identified == false) {
- if (allBunnies[i].gender === "Male") {
- maleBunnies.push(allBunnies[i]);
- } else if (allBunnies[i].gender === "Female") {
- femaleBunnies.push(allBunnies[i]);
- }
- allBunnies[i].identified = true;
- }
- }
- // checks if rabbit is male and over 10 years and makes it get removed from the male group
- for (i = 0; i < maleBunnies.length; i++) {
- if (maleBunnies[i].age > 9) {
- maleBunnies.splice(0, i);
- }
- }
- // checks if rabbit is male and over 10 years and makes it get removed from the female group
- for (i = 0; i < femaleBunnies.length; i++) {
- if (femaleBunnies[i].age > 9) {
- femaleBunnies.splice(0, i);
- }
- }
- }
- function creationTime(time) {
- // making an int value to keep up how much time passes
- var count = 0;
- // Making a function so the page get's updates each milisecond
- var x = setInterval(function() {
- // updates the count value
- count++;
- // creates the bunny after the value reached the specified value in the function argument
- if (count == time) {
- console.clear();
- count = 0;
- createBunny(bunnyNames, gender, color, age, radioActive, identified);
- updateBunny();
- updateStats();
- }
- });
- }
- function updateStats() {
- //
- var total = (document.querySelector(".allbunnies").innerHTML =
- allBunnies.length);
- var male = (document.querySelector(".malebunnies").innerHTML =
- maleBunnies.length);
- var female = (document.querySelector(".femalebunnies").innerHTML =
- femaleBunnies.length);
- var death = (document.querySelector(".deadbunnies").innerHTML = died);
- }
- // Calling out the function to start the program
- creationTime(1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement