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

Another way to approach the problem...is this method okay?

Hi, I am new to JS and I'd love to get some opinions on my code. I created new variables to hold the sum, product, quotient, and difference of the math problems in. Is this approach okay? I was able to get the application running which is great but I want to know if this method is safe to use. Thanks!

var sum = parseFloat(input) + parseFloat(input2); message += input + ' + ' + input2 + ' = ' + sum;

var product = parseFloat(input) * parseFloat(input2); message += input + ' * ' + input2 + ' = ' + product;

var quotient = parseFloat(input) / parseFloat(input2); message += input + ' / ' + input2 + ' = ' + quotient;

var difference = parseFloat(input) - parseFloat(input2); message += input + ' - ' + input2 + ' = ' + difference;

2 Answers

Steven Parker
Steven Parker
243,318 Points

It's quite "safe" but you repeat a lot. You could do the "parseFloat" calls once and save the result in other variables.

And you could compact the code even more with a function. You'll learn more about those in the courses.

Hi Tina, I think your approach is certainly okay and good since it's readable -- the variable names help. The important thing is that your future you and potential future colleague(s) will understand the code. Naming your values is very helpful.

I would approach it on a functional level and use constants instead of variables (when possible). Variables can sometimes lead to unwanted effects -- since they can be reassigned. Also, where reasonable, I would use functions that return something. This will allow simple reuse of code.

This is mainly a functional programming approach I'm currently studying (and enjoying):

const
  input1 = '6', input2 = '4' // assign values
  solve = (mode, x, y) => { 
    // convert values to floats
    x = (+x)
    y = (+y)
   // return evaluation
    return mode === 'add' ? x + y : // if mode is add, then return x + y, else
      mode === 'subtract' ? x - y : // if mode is subtract, then return x - y, else
      mode === 'multiply' ? x * y : // if mode is multiply, then return x * y, else
      mode === 'divide' ? x / y :   // if mode is divided, then return x / y, else
      undefined                     // return undefined
  },

  sum = solve('add', input1, input2),
  difference = solve('subtract', input1, input2),
  product = solve('multiply', input1, input2), 
  quotient = solve('divide', input1, input2), 

  express = (mode, x, y) => {
    return mode === 'add' ? `${x} + ${y} = ${sum}`  : 
      mode === 'subtract' ? `${x} - ${y} = ${difference}`  :  
      mode === 'multiply' ? `${x} * ${y} = ${product}`  : 
      mode === 'divide' ? `${x} / ${y} = ${quotient}`  : 
      undefined                               
  },
  message = `
    ${express('add', input1, input2)}
    ${express('subtract', input1, input2)}
    ${express('multiply', input1, input2)}
    ${express('divide', input1, input2)}
  `
// end const

console.log(message)

This is no doubt overkill for something that otherwise should be short and simple, but the functional concepts will make any program easier to scale and debug. You can run this at ...

:)