Adding Numbers

Good day!

So, I was coding my app, and came across something interesting. If I make a function that asks to multiply two numbers:

function calculate() { var result = input1.value * input2.value / 100; tipAmt.value = “$” + result.toFixed(2);
}

This will produce a multiplied number. Now, if I use the + sign…it concatinates:

function calculate() { var result = input1.value + input2.value; tipAmt.value = “$” + result; }

So my first question is…how can I have two numbers added? And as a bonus question, once I have two numbers added, how can I have another number put in an input, that ADDS on to the previous total? Here’s the code I’m working with now:

var Observable = require(“FuseJS/Observable”); var addInput = Observable(0); var subInput = Observable(0); var calcSub = Observable(0); var totalOz = Observable(0); function addCalc() { var result = addInput.value + totalOz.value; totalOz.value = result; addInput.value = “”; } function clear() { addInput.value = “”; subInput.value = “”; totalOz.value = “”; } addCalc(); clear(); module.exports = { addCalc : addCalc, addInput : addInput, subInput : subInput, calcSub : calcSub, totalOz : totalOz, clear : clear, } + - Clear

Basically, the way this works, is you input a number at the top, it places it on the bottom (totalOz), and any additional number placed in the top field, will keep adding onto the bottom total. Here’s a working version @ codepen: http://codepen.io/0ktane/pen/NNdbOq?editors=0010

I hope this doesn’t sound like a tall order… Just need to know how to add two numbers, and keep that total to be added onto. Thanks for any assistance!

This is JavaScript and you should probably learn that language. + in JavaScript will add with numbers and concat strings. Try to cast it as a string either by using parseInt:

function calculate() { 
    var result = parseInt(input1.value) + parseInt(input2.value); 
    tipAmt.value = "$" + result; 
}

or implicitly like this:

function calculate() { 
    var result = 0 +input1.value + input2.value; 
    tipAmt.value = "$" + result; 
}

I do appreciate the information. As a side note, it came off as rude saying I should learn JS. I already do. If you looked over my post you would see that. I am still new with it though, which is why I asked for help. Though you did help, your reply came off in a negative way, any makes one not want to ask for assistance down the road.