-1

I need help this is really hard to explain I am making a calculator type program. And the main bit where it calculates it wont. it needs to calculate from multiple textboxs and combine them into one readable string. That needs to be run as math. but i can not run it as math, I can only manage to run it as a text. not as equation. is there any way i can change it so it treats it like math. I am a student I am still learning js.

I have tried changing the var name and changing the text function but have not been able to fix it here is the code so you can help:

function add() {
  var value = parseInt(document.getElementById('number').value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    window.value=value;
    document.getElementById('number').value = window.value;
    var num = "v"+window.value;
    var node = document.createElement("a");
    var x = document.createElement("INPUT");
    x.setAttribute("type", "text");
    x.setAttribute("id", num);
    node.appendChild(x);
    document.getElementById("myList").appendChild(node);
  }

function equal() {
  var str = "";
  var ans = "";
for (var i = 1; i <= window.value; i++) {
  str = "v"+i;

this is what i need to be treated as math:

  ans += document.getElementById(str).value;

rest of javascript:

}
document.getElementById("demo3").innerHTML = ans;
}

if it is any help here is the html:

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style.css">

    </head>
<body onload="numnum();"></body>
<input id="number" type="hidden"></input>
<p id="demo"></p>
<p id="demo2"></p>

<p>Click the button to create a Text Field.</p>
<a id="myList"></a>
<button onclick="add()">+</button>
<button onclick="equal()">=</button>
<p id="demo3"></p>
<script src="input.js"></script>
</body>
</html>

thankyou

  • If you're wanting decimal numbers, and want to use the second param on `parseInt` to enforce that, you need to use `parseInt(val, 10)`. That's not your issue, just an observation. – Jacques ジャック May 30 '19 at 04:41
  • fun fact it was set to 10 but i changed it and forgot to put it back thanks for pointing this out ill fix it right away – anormalmouse May 30 '19 at 04:43
  • 2
    Possible duplicate of [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – VLAZ May 30 '19 at 04:43
  • Simply put, `var ans = 0` since you want it to be a number, and then `ans += parseInt(document.getElementById(str).value, 10);` – Jacques ジャック May 30 '19 at 04:45
  • I understand it does look like that i tried the answer from that and it did not work i am not adding 2 together it is one that is a math equation made up of compined text boxes – anormalmouse May 30 '19 at 04:51
  • @Jacques ジャックthat almost worksI will see if I can get it to work – anormalmouse May 30 '19 at 04:55

1 Answers1

-1

Do a simple thing just get all the input as a String and try to split that string into given nos and operations then try to operate with an if else to do your things.You can do any operations.