Back to Freecodecamp

Challenge 162: Energy Consumption

curriculum/challenges/english/blocks/daily-coding-challenges-javascript/694596b0585c11170ac7c7fa.md

latest1.6 KB
Original Source

--description--

Given the number of Calories burned during a workout, and the number of watt-hours used by your electronic devices during that workout, determine which one used more energy.

To compare them, convert both values to joules using the following conversions:

  • 1 Calorie equals 4184 joules.
  • 1 watt-hour equals 3600 joules.

Return:

  • "Workout" if the workout used more energy.
  • "Devices" if the device used more energy.
  • "Equal" if both used the same amount of energy.

--hints--

compareEnergy(250, 50) should return "Workout".

js
assert.equal(compareEnergy(250, 50), "Workout");

compareEnergy(100, 200) should return "Devices".

js
assert.equal(compareEnergy(100, 200), "Devices");

compareEnergy(450, 523) should return "Equal".

js
assert.equal(compareEnergy(450, 523), "Equal");

compareEnergy(300, 75) should return "Workout".

js
assert.equal(compareEnergy(300, 75), "Workout");

compareEnergy(200, 250) should return "Devices".

js
assert.equal(compareEnergy(200, 250), "Devices");

compareEnergy(900, 1046) should return "Equal".

js
assert.equal(compareEnergy(900, 1046), "Equal");

--seed--

--seed-contents--

js
function compareEnergy(caloriesBurned, wattHoursUsed) {

  return caloriesBurned;
}

--solutions--

js
function compareEnergy(caloriesBurned, wattHoursUsed) {
  const workoutEnergy = caloriesBurned * 4184;
  const deviceEnergy = wattHoursUsed * 3600;

  console.log(workoutEnergy);
  console.log(deviceEnergy);
  if (workoutEnergy > deviceEnergy) return "Workout";
  if (deviceEnergy > workoutEnergy) return "Devices";
  return "Equal";
}