Guest User

JavaScript Code

a guest
Mar 28th, 2020
70
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.  
  11.  
  12.  
  13.    
  14.   })
  15.  
  16.     // Add a variable "pet_info" equal to a object with the name (string), weight (number), and happiness (number) of your pet
  17.     var pet_info = {name:"My Pet Name", weight:"100", happiness:"100"};
  18.  
  19.     function clickedTreatButton() {
  20.  
  21.       if document.getElementByClass("treat-button").clicked(){
  22.       // Increase pet happiness
  23.         parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) + 2;
  24.       // Increase pet weight
  25.  
  26.       }
  27.  
  28.       checkAndUpdatePetInfoInHtml();
  29.     }
  30.    
  31.     function clickedPlayButton() {
  32.       if document.getElementByClass("play-button").clicked(){
  33.       // Increase pet happiness
  34.         parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) + 3;
  35.       // decrease pet weight
  36.       parseInt(pet_info.getValue(2)) = parseInt(pet_info.getValue(2)) - 1;        
  37.       }
  38.       checkAndUpdatePetInfoInHtml();
  39.     }
  40.    
  41.     function clickedExerciseButton() {
  42.       if document.getElementByClass("exercise-button").clicked(){
  43.       // Decrease pet happiness
  44.         parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) - 2;
  45.       // Decrease pet weight
  46.         parseInt(pet_info.getValue(2)) = parseInt(pet_info.getValue(2)) - 3;
  47.             }
  48.       checkAndUpdatePetInfoInHtml();
  49.     }
  50.  
  51.     function checkAndUpdatePetInfoInHtml() {
  52.       checkWeightAndHappinessBeforeUpdating();
  53.       updatePetInfoInHtml();
  54.     }
  55.    
  56.     function checkWeightAndHappinessBeforeUpdating() {
  57.       // Add conditional so if weight is lower than zero, set it back to zero
  58.       if pet_info.getValue(1).parseInt() < 0 {
  59.         pet_info.getValue(1).parseInt() = 0;
  60.       }
  61.     }
  62.    
  63.     // Updates your HTML with the current values in your pet_info dictionary
  64.     function updatePetInfoInHtml() {
  65.       $('.name').text(pet_info['name']);
  66.       $('.weight').text(pet_info['weight']);
  67.       $('.happiness').text(pet_info['happiness']);
  68.     }
Add Comment
Please, Sign In to add comment