Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(function() { // Makes sure that your function is called once all the DOM elements of the page are ready to be used.
- // Called function to update the name, happiness, and weight of our pet in our HTML
- checkAndUpdatePetInfoInHtml();
- // When each button is clicked, it will "call" function for that button (functions are below)
- $('.treat-button').click(clickedTreatButton);
- $('.play-button').click(clickedPlayButton);
- $('.exercise-button').click(clickedExerciseButton);
- })
- // Add a variable "pet_info" equal to a object with the name (string), weight (number), and happiness (number) of your pet
- var pet_info = {name:"My Pet Name", weight:"100", happiness:"100"};
- function clickedTreatButton() {
- if document.getElementByClass("treat-button").clicked(){
- // Increase pet happiness
- parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) + 2;
- // Increase pet weight
- }
- checkAndUpdatePetInfoInHtml();
- }
- function clickedPlayButton() {
- if document.getElementByClass("play-button").clicked(){
- // Increase pet happiness
- parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) + 3;
- // decrease pet weight
- parseInt(pet_info.getValue(2)) = parseInt(pet_info.getValue(2)) - 1;
- }
- checkAndUpdatePetInfoInHtml();
- }
- function clickedExerciseButton() {
- if document.getElementByClass("exercise-button").clicked(){
- // Decrease pet happiness
- parseInt(pet_info.getValue(1)) = parseInt(pet_info.getValue(1)) - 2;
- // Decrease pet weight
- parseInt(pet_info.getValue(2)) = parseInt(pet_info.getValue(2)) - 3;
- }
- checkAndUpdatePetInfoInHtml();
- }
- function checkAndUpdatePetInfoInHtml() {
- checkWeightAndHappinessBeforeUpdating();
- updatePetInfoInHtml();
- }
- function checkWeightAndHappinessBeforeUpdating() {
- // Add conditional so if weight is lower than zero, set it back to zero
- if pet_info.getValue(1).parseInt() < 0 {
- pet_info.getValue(1).parseInt() = 0;
- }
- }
- // Updates your HTML with the current values in your pet_info dictionary
- function updatePetInfoInHtml() {
- $('.name').text(pet_info['name']);
- $('.weight').text(pet_info['weight']);
- $('.happiness').text(pet_info['happiness']);
- }
Add Comment
Please, Sign In to add comment