Which example are you referring to that you want to copy?
The way I see it,
I did the research I found the code you are referring to before in this video:
I believe you want a calculator in vanilla js that passes all the tests. However I dont like the example your using, his variable names confuse me and hes doing things not relevant to the challenge i.e. his string manipulations.
Here is a template I think is better to start with:
I did git fork
and afterwards git clone
, The first commit is just changing the id’s etc… to whatever the test is expecting
Results of test -fail #13 :
5*-5 should equal -25
To me, that is saying “five multiplied by negative five should result in -25”
this is solved by changing
evalStringArray.push(pendingVal);
to instead become
if(pendingVal!=0){evalStringArray.push(pendingVal);}
You do this for every operation in the switch except “=” … running the tests again produces the result
Results of test -fail #13 :
“5*-+5” should produce an output of 10
what does that mean “five multiplied by negative plus five”, it does not seem like a correct expression. running this expression on a caluculator on a google phone android device produces the result “invalid math expression”
If anything I would think that it should be -25 (one could argue that the minus sign is stronger than the positive) eval also returns -25.
however if you want to make it return -10 the way I did that was to put this inside the last condition in the switch and define a looping function outside of the switch you will see this in the last commit and it is represented by the following code.
inside your switch:
case '=':
evalStringArray.push(displayVal);
const length=evalStringArray.length
let possibleOperands=evalStringArray.slice(1,length-2)
if(possibleOperands.length>=2&&evalStringArray[length-2]!='-'&&containsOnly(['/','-','+','*','÷'],possibleOperands)){
let newStringArray=[evalStringArray.slice(0,1) , evalStringArray.slice(length-2, length)].flat()
console.log(newStringArray, 'newStringArray')
console.log(possibleOperands, 'possibleOperands')
var evaluation = eval(newStringArray.join(''));
displayVal = evaluation + ''; /*As a string??*/
displayValElement.innerText = displayVal;
evalStringArray = [];
break;
} else {
var evaluation = eval(evalStringArray.join(''));
displayVal = evaluation + ''; /*As a string??*/
displayValElement.innerText = displayVal;
evalStringArray = [];
break;
}
}
custom looping function I used outside of performOperation:
function containsOnly(array1, array2){
console.log('arguments 1',arguments[1])
return array2.every(elem => array1.includes(elem))
}
all the above referenced code can be found here in this cloned repo under their respective commits:
github repo pass challenges
The calculator is now returning -10 as the test specifies for the input “5*-+5” and passing “5*-5” returns -25 as well
Unfortunately It is still not passing #13 its showing me the following error:
and that error is not specific enough for me to understand what it wants. Ask me if anything is unclear. Maybe someone with more exprience in this test can say why its still not succeeding despite the calculator now returning the results that they asked for in the test.