Advertisement
Guest User

JS Code

a guest
Apr 7th, 2020
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() { // Makes sure that your function is called once all the DOM elements of the page are ready to be used.
  2.    
  3.     // Called function to update the name, happiness, and weight of our pet in our HTML
  4.     checkAndUpdatePetInfoInHtml();
  5.  
  6.     // When each button is clicked, it will "call" function for that button (functions are below)
  7.     $('.treat-button').click(clickedTreatButton);
  8.     $('.play-button').click(clickedPlayButton);
  9.     $('.exercise-button').click(clickedExerciseButton);
  10.     $('.reset-button').click(clickedResetButton);
  11.  
  12.  
  13.  
  14.    
  15.   })
  16.  
  17.     // Add a variable "pet_info" equal to a object with the name (string), weight (number), and happiness (number) of your pet
  18.     var pet_info = {name:"Ryan", weight:0, happiness:0};
  19.  
  20.     function clickedTreatButton() {
  21.       // Increase pet happiness
  22.       // Increase pet weight
  23.       pet_info['happiness']+=1;
  24.       pet_info['weight']+=1;
  25.       checkAndUpdatePetInfoInHtml();
  26.     }
  27.    
  28.     function clickedPlayButton() {
  29.       // Increase pet happiness
  30.       // Decrease pet weight
  31.       pet_info['happiness']+=1;
  32.       pet_info['weight']-=1;
  33.       checkAndUpdatePetInfoInHtml();
  34.     }
  35.    
  36.     function clickedExerciseButton() {
  37.       // Decrease pet happiness
  38.       // Decrease pet weight
  39.       pet_info['happiness']-=1;
  40.       pet_info['weight']-=1;
  41.       checkAndUpdatePetInfoInHtml();
  42.     }
  43.  
  44.  
  45.     function resetButton() {
  46.       // Decrease pet happiness
  47.       // Decrease pet weight
  48.       pet_info['happiness']=0;
  49.       pet_info['weight']=0;
  50.       checkAndUpdatePetInfoInHtml();
  51.     }
  52.  
  53.     function checkAndUpdatePetInfoInHtml() {
  54.       checkWeightAndHappinessBeforeUpdating();
  55.       updatePetInfoInHtml();
  56.     }
  57.    
  58.     function checkWeightAndHappinessBeforeUpdating() {
  59.       // Add conditional so if weight is lower than zero, set it back to zero
  60.       if (pet_info['happiness'] <= 0) {
  61.         pet_info['happiness'] = 0;
  62.       }
  63.       if (pet_info['weight'] <= 0) {
  64.         pet_info['weight'] = 0;
  65.       }
  66.     }
  67.    
  68.     // Updates your HTML with the current values in your pet_info dictionary
  69.     function updatePetInfoInHtml() {
  70.       $('.name').text(pet_info['name']);
  71.       $('.weight').text(pet_info['weight']);
  72.       $('.happiness').text(pet_info['happiness']);
  73.       $('.reset').text(pet_info['reset']);
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement