How to solve this problem?

  • 0
    Help solve this problem.

    I have been suffering for two days.
    Here's what I did.

    function fillTank(fuelPrice, amount) {
      const totalPrice = fuelPrice * amount;
      if ( === 0) {
      if (this.maxTankCapacity < this.vehicle.maxTankCapacity) {
         (this.maxTankCapacity / amount) * fuelPrice;
      if (amount === undefined) {
          this.maxTankCapacity * amount;
          return this;
      if ( < this.fuelPrice) {
        Math.random(this.amount -;
      if ( !== parseInt) { = - totalPrice;
      if(amount < 2){

    / ************************************************ ******* /

    Here are the problem conditions and examples.

    Royal Oil is a network of progressive filling stations. Today they want to get rid of routine work and completely automate all processes. We need to create a fillTank function that will be a method of the customer object. The method will take parameters: the price of fuel fuelPrice for one liter, and the amount of liters the amount that the client wants to purchase. In the customer object, there will definitely be a money field, these are the funds that the client has, and the vehicle field - a vehicle, which, in turn, will have the maxTankCapacity and fuelRemains fields. The method should modify the customer object, change the balance on the customer's account, and fill the required amount of fuel into the vehicle.


    const customer = {
      money: 30,
      vehicle: {
        maxTankCapacity: 20,
        fuelRemains: 8,
      refuel: fillTank,
    const fuelPrice = 2;
    const amount = 3;
    customer.refuel(fuelPrice, amount); // 24
    customer.vehicle.maxTankCapacity // 20
    customer.vehicle.fuelRemains // 11

    The function must respect the following rules:

    If the client wants to buy more fuel than the tank of his vehicle holds, fill him with a full tank of fuel and calculate only the cost of the filled fuel.
    If the client does not know how much fuel he needs (the amount parameter is not transmitted), fill the tank full of fuel.
    If the client does not have enough funds on the account, fill in fuel for the amount that the client has, round up liters, discarding everything to a tenth.
    If the customer's account balance is not a whole number, discard everything up to a tenth.
    Fill up with less than 2 liters of fuel.
    Hint: use toFixed to drop the fractional part. Don't forget to convert the string back to a number.
    JavaScript Anonymous, May 21, 2019

  • 3 Answers
  • 0
    There is a contradiction in the assignment. For example, there is:

    vehicle: {
    maxTankCapacity: 10,
    fuelRemains: 9,

    Customer arrived at the gas station and passes only fuelPrice to the refuel method. What is the rule for working? Will we fill the tank full or will we fill nothing?

    If the client does not know how much fuel he needs (the amount parameter was not passed), fill the tank full of fuel.

    You cannot refuel less than 2 liters of fuel.

  • 0
    function fillTank () {

    let maxPurchase = / this.fuelPrice; // how much can buy

    let realTankCapacity = this.maxTankCapacity-this.fuelRemains; // space in the tank

    / * and then we check whether there is enough money for 2 liters or more (the beating is not zero, then we check whether more than 2 liters will fit into the tank and the client's desire should be more than 2 liters.

    then we compare three variables - [1) opportunities to buy 2) space in the tank 3) client's desire] the smaller variable is the amount of fuel that we will fill) * /


    ps You can first calculate three variables, place the smaller one in the volume that you want to fill, and then check if this volume is less than 2 liters, then refuse to refuel.

  • 0
    Yesterday I solved this problem. This way

    Thank you all for your help.

    function fillTank (fuelPrice, amount = this.vehicle.maxTankCapacity - this.vehicle.fuelRemains) {

    let totalPrice = amount * fuelPrice;

    let totalAmount = amount;

    const customerMoney = + (1);

    const maxAmount = this.vehicle.maxTankCapacity - this.vehicle.fuelRemains;

    if (amount & gt; = maxAmount) {

    totalAmount = maxAmount;

    totalPrice = totalAmount * fuelPrice;


    if ( & lt; totalPrice) {

    totalAmount = customerMoney / fuelPrice;

    totalPrice = totalAmount * fuelPrice;


    if (totalAmount & lt; 2) {

    totalAmount = 0;

    totalPrice = 0;

    } = + (customerMoney - totalPrice) .toFixed (1);

    this.vehicle.fuelRemains + = + totalAmount.toFixed (1);

    Jude Cooper

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!