Topic List |
Page List:
1 |
---|---|
LastTomorrow 12/15/17 2:11:40 PM #1: |
function calculator() {
var val1 = theForm.txtval1.value; var val2 = theForm.txtval2.value; var math = document.getElementById("math").value; theForm.Total.value = math; if (math == "add") { document.getElementById("Tol").value = val1 + val2; } if (math == "minus") { document.getElementById("Tol").value = val1 - val2; } if (math == "times") { document.getElementById("Tol").value = val1 * val2; } if (math == "divide") { document.getElementById("Tol").value = val1 / val2; } } When I use it, the subtraction, times, division are correct, but the sum always combines the numbers I entered together. --- Waffle waffle waffle ... Copied to Clipboard!
|
P4wn4g3 12/15/17 2:13:16 PM #2: |
Well if it wasn't JavaScript I'd say check your data types.
--- Hive Mind of Dark Aether, the unofficial Metroid Social Private board. https://www.gamefaqs.com/boards/851-dark-aether ... Copied to Clipboard!
|
FLUFFYGERM 12/15/17 2:13:49 PM #3: |
uh
--- but Marxist theory is extremely consistent, both internally and with reality. -averagejeol ... Copied to Clipboard!
|
LastTomorrow 12/15/17 2:15:54 PM #4: |
<form name="theForm" id="frmcalculator" method="get">
<h1>Simple Calculator</h1> Value 1: <input type="text" id="val1" name="txtval1"><br><br> <select id="math"> <option value="add">+</option> <option value="minus">-</option> <option value="times">*</option> <option value="divide">/</option> </select><br><br> Value 2: <input type="text" id="val2" name="txtval2"><br> <hr> Total: <input type="text" id="Tol" name="Total"><br><br> Here is my HTML --- Waffle waffle waffle ... Copied to Clipboard!
|
DevsBro 12/15/17 2:22:44 PM #5: |
EThe + operator is overloaded in Javascript.
It adds, or if the datatypes are string, it concatenates. "Hello " + "world" = "Hello world". "3" + "2" = "32". Since you're taking the values from a text box, by default the values are strings. Javascript does have datatypes--it just doesn't support variable typing. So what you need to do is cast your values to integers before adding. I don't remember the cast function name but just google "javascript cast int" and you should find it. --- ... Copied to Clipboard!
|
Talks 12/15/17 2:23:43 PM #6: |
... Copied to Clipboard!
|
LastTomorrow 12/15/17 3:01:25 PM #7: |
DevsBro posted...
EThe + operator is overloaded in Javascript. parseInt. So I use that to add everything. --- Waffle waffle waffle ... Copied to Clipboard!
|
DevsBro 12/15/17 3:03:21 PM #8: |
Should work. parseInt("1") + parseInt("2") should give you 3.
--- ... Copied to Clipboard!
|
DevsBro 12/15/17 3:07:49 PM #9: |
This is one beef I have with lots of languages. Using + for concatenation gives you all kinds of simple caveats you can fall into. It's most noticeable in a language lke Javascript but even strongly-typed languages like Java will throw you off if you're still learning or in a hurry or whatever.
System.out.println("The number is " + 3 + 2) doesn't do what you might think. I prefer the way you see it in Ada and VB, where the conctenate operator is & instead. No chance of those kinds of bugs. --- ... Copied to Clipboard!
|
LastTomorrow 12/15/17 3:18:11 PM #10: |
It worked. Thanks dude.
--- Waffle waffle waffle ... Copied to Clipboard!
|
Rexdragon125 12/15/17 3:22:14 PM #11: |
DevsBro posted...
Javascript does have datatypes--it just doesn't support variable typing There is something deeply wrong with people who design languages like this ... Copied to Clipboard!
|
DevsBro 12/15/17 4:43:52 PM #12: |
Rexdragon125 posted...
DevsBro posted...Javascript does have datatypes--it just doesn't support variable typing *shrugs* It generally works pretty well. But like anything, it has its quirks that you have to work around on occasion. --- ... Copied to Clipboard!
|
Topic List |
Page List:
1 |