Advertisement
Danny_Berova

4.Fixed

Nov 21st, 2018
159
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.         let budget = $('input[name=familyBudget]').val();
  37.         let type = $('input[name=familyApartmentType]').val();
  38.         let name = $('input[name=familyName]').val();
  39.  
  40.         //change NB
  41.         $("#message").text('We were unable to find you a home, so sorry :(');
  42.  
  43.         if ($.isNumeric(budget) && budget > 0 && type.trim() && name.trim()) {
  44.  
  45.             for (let offer of offers) {
  46.                 let commission = (offer.commission / 100) * offer.price;
  47.                 if (type === offer.type && budget >= offer.price + commission) {
  48.  
  49.                     //change NB
  50.                     profit += 2 * commission;
  51.  
  52.                     $('#roof h1').text(`Agency profit: ${profit} lv.`);
  53.  
  54.                     //loop through apartments
  55.                     //change NB
  56.                     $(".apartment").find(`p:contains(${type})`).parent().empty()
  57.                         .append(`<p>${name}</p>`)
  58.                         .append('<p>live here now</p>')
  59.                         .append(`<button>MoveOut</button>`)
  60.                         .css("border", "2px solid red");
  61.                    
  62.                         $('#message').text('Enjoy your new home! :))');
  63.                         break;
  64.                 }
  65.  
  66.             }
  67.             //on click remove the apartment
  68.             $('.apartment button').on('click', function () {
  69.                 name = $(this).closest('.apartment').find("p:first").text();
  70.                
  71.                 $(this).closest('.apartment').remove();
  72.                 $('#message').text(`They had found cockroaches in ${name}'s apartment`);
  73.            });
  74.        }
  75.        $('input[name=familyBudget]').val('');
  76.        $('input[name=familyApartmentType]').val('');
  77.        $('input[name=familyName]').val('');
  78.  
  79.    });
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement