<html>
<body>
<input type="text" id="mainbox">
<button onclick="equals()"> </button>
</body>
</html>
<script src="Calculator.js"></script>
var equation = document.getElementById('mainbox').value
function equals(){
var equationSplit = equation.split(/\+\-/);
console.log(equationSplit)
}
Im trying to have the array split based upon + and - signs. I'm currently in the process of just splitting it, but it always returns array length 1.
1) You should move the code of getting the value of the mainbox input value into the equals function, because you want the value after the button is pressed not after the JS is parsed.
var equation = document.getElementById('mainbox').value
2) You can use regex as /[+-]/g
function equals() {
var equation = document.getElementById('mainbox').value
var equationSplit = equation.split(/[+-]/g);
console.log(equationSplit)
}
<input type="text" id="mainbox">
<button onclick="equals()">split</button>
Your regex splits at the first sequence of '+-' which it probably doesn't find in the string.
Make the regex match either one of the characters by itself by wrapping them in [] and set the global flag to make it match multiple occurrences:
var equationSplit = equation.split(/[\+\-]/g);