settingsAccountsettings
Menusettings

Q: Construction Crew (task with JavaScript object composition)

+2 votes

Write a JS program that receives a worker object as a parameter and modifies its properties. Workers have the following structure:

{ weight: Number,
  experience: Number,
  bloodAlcoholLevel: Number,
  handsShaking: Boolean }


Weight is expressed in kilograms, experience in years and bloodAlcoholLevel is in milliliters. If you receive a worker who’s handsShaking property is set to true it means he needs to intake some alcohol in order to be able to work correctly. The required amount is 0.1ml per kilogram per year of experience. The required amount must be added to the existing amount. Once the alcohol is administered, change the handsShaking property to false.

Workers whose hands aren’t shaking should not be modified in any way. Return them as they were.

Input:
Your function will receive a valid object as parameter.

Output:
Return the same object that was passed in, modified as necessary.

Examples:

Sample Input:

{ weight: 80,
  experience: 1,
  bloodAlcoholLevel: 0,
  handsShaking: true }

Output:

{ weight: 80,
  experience: 1,
  bloodAlcoholLevel: 8,
  handsShaking: false }


Sample Input:

{ weight: 120,
  experience: 20,
  bloodAlcoholLevel: 200,
  handsShaking: true }

Output:

{ weight: 120,
  experience: 20,
  bloodAlcoholLevel: 440,
  handsShaking: false }


Sample Input:

{ weight: 95,
  experience: 3,
  bloodAlcoholLevel: 0,
  handsShaking: false }

Output:

{ weight: 95,
  experience: 3,
  bloodAlcoholLevel: 0,
  handsShaking: false }

asked in JavaScript category by user ak47seo

1 Answer

+1 vote

My code:

function modifyWorker(worker) {
    if (worker.handsShaking == true) {
        worker.bloodAlcoholLevel += worker.experience * worker.weight * 0.1;
        worker.handsShaking = false;
    }
    return worker;
}

console.log(modifyWorker({
        weight: 80,
        experience: 1,
        bloodAlcoholLevel: 0,
        handsShaking: true
    }
));

 

answered by user andrew
...