Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. class bunny {
  2. constructor(name, gender, color, age, radioActive, identified) {
  3. this.name = name;
  4. this.gender = gender;
  5. this.color = color;
  6. this.age = age;
  7. this.radioActive = radioActive;
  8. this.identified = identified;
  9. }
  10. }
  11.  
  12. // creating variables for the bunny's data
  13. var bunnyNames = [
  14. "Sleepy",
  15. "Flappy",
  16. "Clurp",
  17. "Trup",
  18. "Siuce",
  19. "Mario",
  20. "Ash",
  21. "Wurppy",
  22. "Tillo",
  23. "Shivvy"
  24. ];
  25.  
  26. var gender = ["Male", "Female"];
  27. var color = ["White", "Brown, 'Black", "Spotted"];
  28. var age;
  29. var radioActive = false;
  30. var identified = false;
  31.  
  32. var allBunnies = [];
  33. var maleBunnies = [];
  34. var femaleBunnies = [];
  35.  
  36. var died = 0;
  37.  
  38. function createBunny(name, gender, color, age, radioActive, identified) {
  39. // gets each array and changes the value to a randomized value
  40. name = name[Math.floor(Math.random() * name.length)];
  41. gender = gender[Math.floor(Math.random() * gender.length)];
  42. color = color[Math.floor(Math.random() * color.length)];
  43. age = 0;
  44.  
  45. radioActive = false;
  46. identified = false;
  47.  
  48. if (Math.random() * 10 < 0.02 + 1) radioActive = true;
  49. // 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
  50. var Newbunny = new bunny(name, gender, color, age, radioActive, identified);
  51. // Pushes the made bunny into an array
  52. allBunnies.push(Newbunny);
  53. console.log(name + ` Has been born he's ${age}`);
  54. for (i = 0; i < maleBunnies.length; i++) {
  55. console.log("Name " + maleBunnies[i].name + " Age: " + maleBunnies[i].age);
  56. }
  57. }
  58.  
  59. function updateBunny() {
  60. // aging each rabbit, so they get older
  61. for (i = 0; i < allBunnies.length; i++) {
  62. allBunnies[i].age = allBunnies[i].age + 1;
  63. // checks if rabbit is over 10 years and makes it get removed
  64. if (allBunnies[i].age > 9) {
  65. console.log(allBunnies[i].name + " Has died ");
  66. allBunnies.splice(0, i);
  67. died = died + 1;
  68. }
  69. // checks each rabbit's gender to put them into different groups
  70. if (allBunnies[i].identified == false) {
  71. if (allBunnies[i].gender === "Male") {
  72. maleBunnies.push(allBunnies[i]);
  73. } else if (allBunnies[i].gender === "Female") {
  74. femaleBunnies.push(allBunnies[i]);
  75. }
  76. allBunnies[i].identified = true;
  77. }
  78. }
  79. // checks if rabbit is male and over 10 years and makes it get removed from the male group
  80. for (i = 0; i < maleBunnies.length; i++) {
  81. if (maleBunnies[i].age > 9) {
  82. maleBunnies.splice(0, i);
  83. }
  84. }
  85. // checks if rabbit is male and over 10 years and makes it get removed from the female group
  86. for (i = 0; i < femaleBunnies.length; i++) {
  87. if (femaleBunnies[i].age > 9) {
  88. femaleBunnies.splice(0, i);
  89. }
  90. }
  91. }
  92.  
  93. function creationTime(time) {
  94. // making an int value to keep up how much time passes
  95. var count = 0;
  96. // Making a function so the page get's updates each milisecond
  97. var x = setInterval(function() {
  98. // updates the count value
  99. count++;
  100. // creates the bunny after the value reached the specified value in the function argument
  101. if (count == time) {
  102. console.clear();
  103. count = 0;
  104. createBunny(bunnyNames, gender, color, age, radioActive, identified);
  105. updateBunny();
  106. updateStats();
  107. }
  108. });
  109. }
  110.  
  111. function updateStats() {
  112. //
  113. var total = (document.querySelector(".allbunnies").innerHTML =
  114. allBunnies.length);
  115. var male = (document.querySelector(".malebunnies").innerHTML =
  116. maleBunnies.length);
  117. var female = (document.querySelector(".femalebunnies").innerHTML =
  118. femaleBunnies.length);
  119. var death = (document.querySelector(".deadbunnies").innerHTML = died);
  120. }
  121.  
  122. // Calling out the function to start the program
  123. creationTime(1000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement