Advertisement
Guest User

Task 4. Real Estate Agency 100/100

a guest
Nov 21st, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function realEstateAgency() {
  2.  
  3.     let offers = [];
  4.     let profit = 0;
  5.  
  6.     $('button[name=regOffer]').on('click', function () {
  7.         let price = $('input[name=apartmentRent]').val();
  8.         let type = $('input[name=apartmentType]').val();
  9.         let commission = $('input[name=agencyCommission]').val();
  10.        
  11.         if ($.isNumeric(price) && $.isNumeric(commission) && commission >= 0 && commission <= 100 && price>0 && type.trim() && !type.includes(":")) {
  12.  
  13.             $('#building').append('<div class="apartment">');
  14.             $('.apartment').last().append(`<p>Rent: ${price}<p>Type: ${type}<p>Commission: ${commission}`);
  15.  
  16.             $('#message').text('Your offer was created successfully.');
  17.             offers.push({
  18.                 price,
  19.                 type,
  20.                 commission
  21.             });
  22.         } else {
  23.             $('#message').text('Your offer registration went wrong, try again.');
  24.         }
  25.  
  26.         $('input[name=apartmentRent]').val('');
  27.         $('input[name=apartmentType]').val('');
  28.         $('input[name=agencyCommission]').val('');
  29.         price = '';
  30.         type = '';
  31.         commission = '';
  32.  
  33.     });
  34.  
  35.     $('button[name=findOffer]').on('click', function () {
  36.        
  37.         let budget = $('input[name=familyBudget]').val();
  38.         let type = $('input[name=familyApartmentType]').val();
  39.         let name = $('input[name=familyName]').val();
  40.  
  41.        
  42.         if(!$("#building").hasClass("apartment")){     
  43.             $("#message").text("We were unable to find you a home, so sorry ??")
  44.         }
  45.        
  46.         if ($.isNumeric(budget) && budget > 0 && type.trim() && name.trim()) {
  47.  
  48.             for (let offer of offers) {
  49.                 let commission = (offer.commission / 100) * offer.price;
  50.                 if (type === offer.type && budget >= offer.price + commission) {
  51.                 profit += 2 * commission;
  52.                     $('#roof h1').text(`Agency profit: ${profit}lv.`);
  53.  
  54.                     //loop through apartments
  55.                     $(".apartment").find(`p:contains(${type})`).parent().empty()
  56.                         .append(`<p>${name}<p>live here now</p><button>MoveOut</button`)
  57.                         .css("border", "2px solid red");
  58.                    
  59.                         $('#message').text('Enjoy your new home! :))');
  60.                         break;
  61.                 } else {
  62.                     $("#message").text("We were unable to find you a home, so sorry :(")
  63.                 }
  64.  
  65.             }
  66.             //on click remove the apartment
  67.             $('.apartment button').on('click', function () {
  68.                 name = $(this).closest('.apartment').find("p:first").text();
  69.                
  70.                 $(this).closest('.apartment').remove();
  71.                 $('#message').text(`They had found cockroaches in ${name}'s apartment`);
  72.             });
  73.         }
  74.         $('input[name=familyBudget]').val('');
  75.         $('input[name=familyApartmentType]').val('');
  76.         $('input[name=familyName]').val('');
  77.  
  78.     });
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement