javascriptbeginner10 minutes

Refactor and Simplify a Function to Calculate Total Price

Improve the readability and maintainability of a simple JavaScript function that calculates the total price of items including tax. The functionality should remain the same while making the code cleaner.

Challenge prompt

You are given a function that calculates the total price for a purchase including tax. The current function works correctly but is written in a verbose and less readable way. Refactor the function to make it simpler and easier to understand, without changing its behavior.

Guidance

  • Keep the function's input parameters and output the same.
  • Remove unnecessary variables and simplify expressions where possible.
  • Use descriptive variable names to clarify meaning.

Hints

  • Look for opportunities to combine multiple statements into one expression.
  • Consider removing redundant intermediate variables.
  • Use arithmetic operations directly in the return statement if possible.

Starter code

function calculateTotalPrice(price, taxRate) {
  let taxAmount = price * taxRate;
  let total = price + taxAmount;
  return total;
}

Expected output

calculateTotalPrice(100, 0.15) === 115

Core concepts

functionsvariablesarithmetic operationscode readability

Challenge a Friend

Send this duel to someone else and see if they can solve it.