I'm trying to find a validator in JS to check if a operator has been used or not ( we're making a simple calculator)so I tried to make a Function checkOperator()
, But it was a dead-end and when I used it, it doesnt do anything.
function checkOperator(){
var operation = document.getElementsByClassName("operator");
var string = document.getElementsByName('show');
var lastChar = string.charAt(string.length-1);
if(lastChar === operation){
var restring =string.replace(operation,operation);
return restring;
}
}
<div id="cursor" class="rekenmachine">
<form name="rekenmachine">
<div>
<input id="cursor" type="text" name="show" value="">
<input id="cursor" type="button" name="Clear" value="C" onclick="rekenmachine.show.value =''">
</div>
<div class="input">
<input id="cursor" type="button" value="7" onclick="rekenmachine.show.value +='7'">
<input id="cursor" type="button" value="8" onclick="rekenmachine.show.value +='8'">
<input id="cursor" type="button" value="9" onclick="rekenmachine.show.value +='9'">
<input id="cursor" type="button" class="operator" value="/" onclick="rekenmachine.show.value +='/'">
</div>
</form>
</div>
I expected to only be able to Put one operator, since Eval can't calculate the string if there are multiple operators next to each other.