Skip to main content
deleted 18 characters in body
Source Link
konijn
  • 33.8k
  • 5
  • 69
  • 264

From a short review;

  • You are dealing with amounts, but you call your variable num1 and num2
  • Your code now has tons of magic numbers, you should use named constants
  • I understand you are not yet in to 'functions within functions', but that is how you apply DRY in this case

So the DRY version could look like this;

function addSurcharge(amount){
  const FLOOR = 10;
  const CEILING = 20;

  if (amount <= FLOOR) {
    return amount + 1;
  } else if (amount > FLOOR && amount<= CEILING) {
    return amount + 2;
  } else {
    return amount + 3;
  }    
}

function addWithSurcharge(amount1, amount2) {
  return addSurcharge(amount1) + addSurcharge(amount2);
}

From a short review;

  • You are dealing with amounts, but you call your variable num1 and num2
  • Your code now has tons of magic numbers, you should use named constants
  • I understand you are not yet in to 'functions within functions', but that is how you apply DRY in this case

So the DRY version could look like this;

function addSurcharge(amount){
  const FLOOR = 10;
  const CEILING = 20;

  if (amount <= FLOOR) {
    return amount + 1;
  } else if (amount > FLOOR && amount<= CEILING) {
    return amount + 2;
  } else {
    return amount + 3;
  }    
}

function addWithSurcharge(amount1, amount2) {
  return addSurcharge(amount1) + addSurcharge(amount2);
}

From a short review;

  • You are dealing with amounts, but you call your variable num1 and num2
  • Your code now has tons of magic numbers, you should use named constants
  • I understand you are not yet in to 'functions within functions', but that is how you apply DRY in this case

So the DRY version could look like this;

function addSurcharge(amount){
  const FLOOR = 10;
  const CEILING = 20;

  if (amount <= FLOOR) {
    return amount + 1;
  } else if (amount<= CEILING) {
    return amount + 2;
  } else {
    return amount + 3;
  }    
}

function addWithSurcharge(amount1, amount2) {
  return addSurcharge(amount1) + addSurcharge(amount2);
}
Source Link
konijn
  • 33.8k
  • 5
  • 69
  • 264

From a short review;

  • You are dealing with amounts, but you call your variable num1 and num2
  • Your code now has tons of magic numbers, you should use named constants
  • I understand you are not yet in to 'functions within functions', but that is how you apply DRY in this case

So the DRY version could look like this;

function addSurcharge(amount){
  const FLOOR = 10;
  const CEILING = 20;

  if (amount <= FLOOR) {
    return amount + 1;
  } else if (amount > FLOOR && amount<= CEILING) {
    return amount + 2;
  } else {
    return amount + 3;
  }    
}

function addWithSurcharge(amount1, amount2) {
  return addSurcharge(amount1) + addSurcharge(amount2);
}