Need help passing tests 10 and 11 in the Javascript calculator

I attempted to use regex to try and replace multiple zeros occurring at the start of a number with a single zero and multiple decimal elements with a single decimal element

Please help me see where i am going wrong

Here is a link to my code:

Hello there,

I am just wondering why you have seperate regexes to match the same thing:

/(^0)\1{2,}/
/^0+/

And

/(\.)\1{2,}/
/\.+/

I would change the regex you have in the match methods to be the same as those in the replace. I do not see why you would match one thing, and replace another.

Before i was trying to replace the unneeded characters with an empty string. Hence the regexes being different :sweat_smile: :sweat_smile:

This makes more sense now
Though it’s throwing an error

calc.input.match is not a function

I figured the String.prototype.match function would work since my input is in string format

Edit:
I changed calc.input.replace(/0)\1{2,}/, 0) to calc.input.replace(/0)\1{2,}/, "0")

Still getting the error

Why do you assume it is always a string? You begin by setting its initial state to a number. So, if it changes to anything but if (calc.input === 0) {, and it is not a string, the error will come up.

I’m using the evaluate function from the mathjs library. The function takes in a string as a parameter. That’s why i assume it is always a string.

I’ve reset the initial state to a string and the condition for my first if statement to a string as well. Thank you for your input so far

Ah, this would again cause problems because my click event

Edit:
changing the initial state and condition to string means i also have to change the value being passed when the click event is executed. Thank you for this. I will try that and see what happens

1 Like