All tests are passing except #13. Please help l am stuck for over a week.
Your code so far
var displayText=document.getElementById(`display`);//Getting the display element
displayText.textContent=`0`;//Setting the display area's text to 0
const clearDisplayDiv=()=>{//Function to clear the display div
displayText.textContent=``;//Clearing the display div
displayText.textContent=`0`; //Setting the initial value to 0
}
var is_operator=false;
const getNumbers=(event)=>{//function for inputting digits
if (displayText.textContent===`0`) {//checking if the display div is empty
displayText.textContent=event.textContent;//assigning the clicked key's text content
} else if (is_operator) {
is_operator = false;
displayText.textContent = event.textContent;
} else if (displayText.textContent.includes(".")) {//To handle decimal inputs
displayText.textContent = displayText.textContent + "" + event.textContent.replace(".", "");//Avoiding 2 consequitive dots
} else {
displayText.textContent = displayText.textContent + "" + event.textContent;
}
}
const getOperators=(event)=>{// function to input operators
let lastElement = expression[expression.length - 1];
if (["/", "*", "+", "-"].includes(lastElement) && is_operator) {//checking if the last element in the array is an operator
expression.push(event.textContent);//adding the operator clicked to the end of the array
} else {
expression.push(displayText.textContent);
expression.push(event.textContent);
}
is_operator = true;
}
var expression=[];//declaring and initialising the array to hold the aquation to be calculated
const calculateResult=()=>{// function to calculate the result
try{//Try block to perform the calculation/evaluation
expression.push(displayText.textContent);
displayText.textContent = eval(expression.join(""));//evaluating/calculating the expression
expression = [];//setting the expression to empty in preparationg for the next calculation
}
catch(event){//Catch bock to catch and display error message
displayText.textContent="ERR TRY AGAIN";//Error message to be outputted
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Challenge Information:
Front End Development Libraries Projects - Build a JavaScript Calculator
For this project you should use a frontend framework.
If you are using the freeCodeCamp editor, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.
The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.
The forum needs to inspect all of your code for debugging.
You are more likely to get help if you provide a live demo on something like Codepen, Stackblitz, Codesandbox, etc.
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 * (-5) ).
Maybe I’m reading your code wrong, but are you not always adding the operator to the expression array? And if so, are you not seeing an error?