Background: In Finland the conventional grading system in schools is based on numbers and minus/plus signs. If a student gets 6+, that equals to 6.25, and 6- is equal to 5.75.
I have the following code:
function miinustin() {
var vanha = $('#summa').val();
var valiUusi = vanha.slice(0, -1);
var vikamerkki = vanha.slice(-1);
if (vikamerkki === "-") {
var uusi = valiUusi - 0.25;
$('#summa').val(uusi);
} else if (vikamerkki === "+") {
var uusi = valiUusi + 0.25;
$('#summa').val(uusi);
} else if (vikamerkki === "½") {
var uusi = valiUusi + 0.5;
$('#summa').val(uusi);
}
};
$('#summa').on("input", miinustin);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="" id="summa">
Why does 6- become 5.75 but 6+ becomes 60.25 and 6½ becomes 60.50, and how do I overcome this problem? I do realise that for some reason in the else if statements 0.25 and 0.50 are not considered numeric values, but why?
+is not always an arithmetic operator in JavaScript.