So i'm writing a calculator and I'm struggling with the equal sign, I'm trying to do some simple bedmas (no brachets and no exponents), but my code keeps returning 0 as if the if statment ar egetting totally skipped
I have no idea what is causing this as I'm getting no errors
function equal(){
let exp = document.form.textview.value;
let expArray = exp.split(/\b/);
console.log(expArray); //<-if something doesn't work check this
let total = 0;
var lastOperator = "+";
var lastDig = 0;
for(let i = 0 ; i < expArray.length; i++){
var dig = expArray[i].trim(); //value of "i", the item currently in the loop
if(isNaN(dig) == true){
console.log("string")
}
else if(isNaN(dig) == false){
console.log('not string');
if (dig == "/"){ //this whole section gets skipped :(
var a = i + 1;
var p = i - 1;
var aDig = parseFloat(expArray[a].trim); //value after "i"
var bDig = parseFloat(expArray[p].trim); // value before "i"
var sDig = aDig / bDig;
total = expArray.splice(bDig, 3, sDig);
}
else if ( dig == "*"){
var a = i + 1;
var p = i - 1;
var aDig = parseFloat(expArray[a].trim); //value after "i"
var bDig = parseFloat(expArray[p].trim) // value before "i"
var sDig = aDig * bDig;
total = expArray.splice(bDig, 3, sDig);
}
else if ( dig == "+"){
var a = i + 1;
var p = i - 1;
var aDig = parseFloat(expArray[a].trim); //value after "i"
var bDig = parseFloat(expArray[p].trim)// value before "i"
var sDig = aDig + bDig;
total = expArray.splice(bDig, 3, sDig);
console.log(expArray.splice(bDig, 3, sDig));
}
else if( dig == "-"){
var a = i + 1;
var p = i - 1;
var aDig = parseFloat(expArray[a].trim); //value after "i"
var bDig = parseFloat(expArray[p].trim) // value before "i"
var sDig = aDig - bDig;
total = expArray.splice(bDig, 3, sDig);
}
}
}
document.form.textview.value = total;
} ```
Aucun commentaire:
Enregistrer un commentaire