Front End Development Libraries Projects - Build a JavaScript Calculator

Tell us what’s happening:

Weird test results and problem with story 13. I’m struggling

I’ve been tossing all I’ve got at story 13 and couldn’t figure out how to get it to pass, I’ve also encountered weird test results as for example story 9 wouldn’t pass when I tried to host it locally through nodeJS while it passed in the codepen linked bellow and story 15 would pass locally but didn’t in codepen. This post is basically me throwing in the towel.

Your code so far

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0

Challenge Information:

Front End Development Libraries Projects - Build a JavaScript Calculator

What do you mean by “weird test results”? I just manually tested the input for test 13:

5 * - + 5

And your calculator gave an answer of 25 instead of the expected 10. This doesn’t seem weird, just wrong. Your calculator should only be using the last operator, so it should be treating that as 5 + 5. Where in your code are you doing this?

I’m not talking about problem 13, I know I have that wrong I’ve struggling to do that part. I’m confused about why some tests were failing when I hosted it locally but didn’t fail when I imported the code into CodePen. Here is the code for the code handling operators, it starts at line 28 in the linked Codepen.

const handleOperatorClick = (operator) => {
		const opRegex = /[/*+-]/;
		const lastOp = calcData.slice(calcData.length - 2);
		console.log(`Current Op: ${operator}`);
		console.log(`Last Op: ${lastOp}`);
		if (opRegex.test(lastOp) && operator !== "-") {
			setCalcData(`${calcData.slice(0, calcData.length - 2)} ${operator} `);
		} else if (operator === "-" && lastOp === "-") {
			setCalcData(`${calcData.slice(0, calcData.length - 2)} + `);
		} else {
			setCalcData(calcData + " " + operator + " ");
		}
	};

Sorry, if my description about the problem was unclear