The Calculator project {[So-Hard]}

Git hub royalgreen50

So I have look at this over and over and I can’t find why my equals will not work. Have a look and happy coding.

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Build a JavaScript Calculator

Link to the challenge:

So I got my equals button to work now I can get user number 7. It just never ends.

  1. At any time, pressing the clear button clears the input and output values, and returns the calculator to its initialized state; 0 should be shown in the element with the id of “display”

your equals button is working and I see test #7 is also passing. so… do you still want help with this?

yes please. i don’t understand unless it’s the ID “display “
but i have it in my html just not using it. may in css but i use data for js. idk.

for some reason it is all working but 12 now. :man_shrugging:

1 Like

so i tried using switch, but it didn’t work . But it has to be it .
So i need help please.

check your inbox. pm sent

New Link

just can’t see how to make my last user story to work.

User Story #13: If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign). For example, if 5 + * 7 = is entered, the result should be 35 (i.e. 5 * 7); if 5 * - 5 = is entered, the result should be -25 (i.e. 5 x (-5)).

I may just copy the example one idk I feel like a loser.

I want to finish this one and make the key pad active.

i’m just not getting it . :roll_eyes:

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.

Sorry, I’ve been away I had to take a break. I know you are helping and you are very helpful. I’m just slow My nickname is “Sloth” and I’m fine with that. So now I back let’s crack this whip. :smiley:

so that doesn’t work I think I just need to change the value of “-”
so it’s and operator when it needs to be and subtract when it needs to.
I just don’t know how to tell my computer to do it.