Cannot pass 11,13 test of the javascript calculator project of Frontend libraries projects

Help me pass this test.!!!

11. When the decimal element is clicked, a “.” should append to the currently displayed value; two “.” in one number should not be accepted.

If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

These are the two tests that I am failing.

Here’s my code

So when I input things like 8*-2, your calculator replaces the * with -, but the test suite want you to treat the - as a negative sign if it is the second operator.

When I input things like 8.8.8.8.8.8 your calculator accepts that as a valid number,presumably because you only check if the last digit is a decimal.

i tried this for the 11th test but its incomplete. any suggestions what should i do to make it work?

if(arr.join(’’).replace(/[^.]/g,"").length>1){

      //i don't know what to write in here 

    }else{
        $('#display').text(arr.join(''))
    }

what you can do is further split your input into the portion before operator (left operand), and the portion after (right operand).
so something like: 45.67x22.10, you split based on a regex that matches all valid operators into left operand: 45.67 and right operand: 22.10.
45.67 is irrelevant here because the user isn’t modifying that number anymore, so just keep the right operand: 22.10.
Now try to find if decimal point already exists in 22.10, if it does, reject the attempt to add more decimals.

Edge case to consider: what if there is no operator?

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.