Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Optimize my code

I have written a simple calculator script and see duplicate code in it, how can I optimize it to be more DRY?

function Calculator() { }

Calculator.divide = function(arg1, arg2) { return (arg1 / arg2); }

Calculator.multiply = function(arg1, arg2) { return (arg1 * arg2); }

Calculator.add = function(arg1, arg2) { return (arg1 + arg2); } Calculator.subtract = function(arg1, arg2) { return (arg1 - arg2); }

do { var value1; var value2; var i = true; value1 = prompt('Enter 1st number'); value2 = prompt('Enter 2nd number');

var calcType = prompt('Enter a=add, s=subtract, d=divide, m=multiply, q=quit'); if (calcType.toLowerCase() === "a") { var answer = Calculator.add( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "s") { var answer = Calculator.subtract( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "d") { var answer = Calculator.divide( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "m") { var answer = Calculator.multiply( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer);
} else if (calcType.toLowerCase() === "q") { break;
} else { alert('You did not choose a correct arithmetic operator, try again.'); } } while (i)

function Calculator() { }

Calculator.divide = function(arg1, arg2) { return (arg1 / arg2); }

Calculator.multiply = function(arg1, arg2) { return (arg1 * arg2); }

Calculator.add = function(arg1, arg2) { return (arg1 + arg2); } Calculator.subtract = function(arg1, arg2) { return (arg1 - arg2); }

do { var value1; var value2; var i = true; value1 = prompt('Enter 1st number'); value2 = prompt('Enter 2nd number');

var calcType = prompt('Enter a=add, s=subtract, d=divide, m=multiply, q=quit'); if (calcType.toLowerCase() === "a") { var answer = Calculator.add( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "s") { var answer = Calculator.subtract( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "d") { var answer = Calculator.divide( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer); } else if (calcType.toLowerCase() === "m") { var answer = Calculator.multiply( parseInt(value1), parseInt(value2) ); alert('The answer is: ' + answer);
} else if (calcType.toLowerCase() === "q") { break;
} else { alert('You did not choose a correct arithmetic operator, try again.'); } } while (i)

1 Answer

Try doing contructor with methods it will help you clean up your code a bit.

CHANGES stated at the bottom

do {
value1 = parseInt(prompt("Enter 1st number: "));
value2 = parseInt(prompt("Enter 2nd number: "));
} while(isNaN(value1) === true 
     || isNaN(value2) === true);

function Calculator(num1, num2) {
  this.input1 = num1;
  this.input2 = num2;

  this.divide = function() {
    return this.input1/this.input2;
  }
  this.multiply = function() {
    return this.input1*this.input2;
  }
  this.addition = function() {
    return this.input1+this.input2;
  }
  this.subtraction = function() {
   return this.input1-this.input2;
  }
}

var calc = new Calculator(value1, value2);
var sum = calc.addition();
var product = calc.multiply();
var quotient = calc.divide();
var difference = calc.subtraction();
var calcBool = false;

while(calcBool != true) {
  var calcType = prompt("Enter a=add, s=subtract, d=divide, m=multiply, q=quit").toLowerCase();
  switch(calcType) {
    case 'a':
      alert("The answer is: " + sum);
      calcBool = true;
      break;
    case 's':
      alert("The answer is: " + difference);
      calcBool = true;
      break;
    case 'd':
      alert("The answer is: " + quotient);
      calcBool = true;
      break;
    case 'm':
      alert("The answer is: " + product);
      calcBool = true;
      break;
    case 'q':
      break;
    default:
      alert("You did not choose a correct arithmetic operator, try again.");
      break;
   }
}

For more efficient coding you can also use isNaN function search for it, it will help alot! ;)

Changes I've made:

  • value2 = parseInt(prompt("Enter 2nd number: ")); enclosed the prompt with parseInt to avoid repitition of parseInt in latter part of the code.
  • used switch instead of if/else if to make the code more readable and to shorten the code.
  • did some contructor with methods to make the code look better(atleast for me, My Habit);
  • use isNaN in prompting value1 and value2 to ensure that the user will only input numbers.
  • prompt("Enter a=add, s=subtract, d=divide, m=multiply, q=quit").toLowerCase(); so that I won't repeat toLowerCase() in the switch case.

REFERENCE: https://www.codecademy.com/courses/spencer-sandbox/0/1?curriculum_id=506324b3a7dffd00020bf661

Thank you so much for your answer Ezekiel, it has proven most insightful.