Learn Functional Programming by Building a Spreadsheet - Step 74

Well, I’m stuck here and I don’t understand what the problem could be.

Problem:

In your highPrecedence function, declare a regex variable.
Assign it a regular expression that matches a number
(including decimal numbers) followed by a * or / operator
followed by another number.

Each number, and the operator, should be in
separate capture groups.

Solution:

const highPrecedence = str => {
  const regex = /(\d*\.?\d+)\s*([\*\/])\s*(\d*\.?\d+)/;
}

Response:

Your first capture group should use a character class.

But in my solution there are A LOT of character class! Maybe the problem is the optional spaces…

const highPrecedence = str => {
  const regex = /(\d*\.?\d+)([\*\/])(\d*\.?\d+)/;
}

Response:

Your first capture group should use a character class.

Ok, maybe the problem is the “type” of character class:

const highPrecedence = str => {
  const regex = /([0-9]*\.?[0-9]+)([\*\/])([0-9]*\.?[0-9]+)/;
}

No. The response is:

Your first capture group should match any digit or a period.
Use the special \d character class.

Ok. I would just know what exactly the problem is asking to me.

I have prepared some tests to demonstrate that my solution works as expected, but it is not accepted by the system.

const highPrecedence = str => {
  const regex = /^(\d*\.?\d+)\s*([\*\/])\s*(\d*\.?\d+)$/;
  return regex.test(str);
}
console.log(highPrecedence(12*34)) // false
console.log(highPrecedence('12*34')) // true
console.log(highPrecedence('12*3.4')) // true
console.log(highPrecedence('.2*3.4')) // true
console.log(highPrecedence('0.2 / .4')) // true
console.log(highPrecedence('0.2 / 23,5')) // false

But the response from the system is always: Your regex should use a capture group.

Why this happens?

1 Like

I’m not very familiar with this course, so what I’m about to write may be wrong, but to me, your answer looks pretty dang good and seems to satisfy the requirements of the instructions. So I cheated and looked at step 75 to see what the tests are expecting, and I was surprised by the answer. The official answer allows for multiple decimal points in a number. It even allows for only a decimal point and no number at all. And, it doesn’t allow for spaces between the numbers and the operator.

Like I said, I’m not very familiar with this course, so maybe there is a reason the official answer is written the way it is? Hopefully what I wrote above is enough to help you figure out what the tests are expecting, but if not, the other hint I will give you is that your final answer should have exactly three sets of parentheses, with exactly one set of square brackets inside each set of parentheses.

const highPrecedence = str => {
  const regex = /^(\d*[.]*\d*)([\*\/])(\d*[.]*\d*)$/;
  return regex.test(str);
}
// ... allows for multiple decimal points in a number
console.log(highPrecedence('23..4*6..7')) // true
// ... allows for only a decimal point and no number at all
console.log(highPrecedence('.*6..7')) // true
// ... it doesn’t allow for spaces between the numbers and the operator
console.log(highPrecedence('33 /67')) // false

The fourth requirement (“your final answer should have exactly three sets of parentheses, with exactly one set of square brackets inside each set of parentheses”) is also respected, even if the resulting regex is somewhat bizarre.

In any case, the system response does not change: Your regex should use a capture group.

However, it seemed to me that there is an expected answer, whatever it may be, somewhere.

It’s disheartening to have to cheat, but if there’s no other solution…

In this case, I wouldn’t consider this cheating.

Unfortunately, this is one of the few projects I am least familiar with but there have been a few posts about expected answers not matching up with the directions and hints always.

So I would suggest opening up an issue

1 Like

Has this issue been resolved?
I also had problems with this step.

After more than several failed attempts, I decided to break down my thought process into traceable executions.

The instruction asks for 3 capture groups. So:

const regex = /(number1)(operator)(number2)/;

, where “number1”, “operator”, and “number2” are placeholders for each capture group.

Now, try capturing non-decimal numbers and the operator as instructed:

  1. Use a character class \d.
  2. The operator can either be * or /.
  3. Number2 should follow the same pattern of Number1.

const regex = /(\d)([\*\/])(\d)/;

Numbers can be more than one digit. So:

const regex = /(\d+)([\*\/])(\d+)/;

Per instruction, include decimal numbers.
Since decimal numbers are optional captures, modify regex as follows:

const regex = /(\d+(\.\d+)?))([\*\/])(\d+(\.\d+)?))/;

Since there may or may not be whitespace between numbers and operator, modify regex as follows:

const regex = /(\d+(\.\d+)?))\s*([\*\/])\s*(\d+(\.\d+)?))/;

If I submit this answer, I get:

Screenshot 2024-04-19 at 13-20-27 freeCodeCamp.org

I struggled with understanding what this hint message means.
My submitted answer contains 3 capture groups.
The first capture group does use a character class, namely \d.
So, I do not understand what the tests are looking for.

But I want to go on. So, I try different answers to figure out - practically guessing - what the tests are looking for.

First try:

const regex = /([0-9]+(\.[0-9]+)?)\s*([\*\/])\s*([0-9]+(\.[0-9]+)?/;

Submitting this answer, I get:

Screenshot 2024-04-19 at 13-19-43 freeCodeCamp.org

Okay, the character class [0-9] is a no-go for this test.
Understandable, since \d is simpler.

Second try: \d* instead of \d+

const regex = /(\d*(\.\d*)?))\s*([\*\/])\s*(\d*(\.\d*)?))/;

Third try: Removing \s* between numbers and operator

const regex = /(\d*(\.\d*)?))([\*\/])(\d*(\.\d*)?))/;

const regex = /(\d+(\.\d+)?))([\*\/])(\d+(\.\d+)?))/;

The second and the third try all get:

Screenshot 2024-04-19 at 13-20-27 freeCodeCamp.org

I feel helpless at this point, because this hint message is not helpful - not in the slightest.

I looked up “ask for help” in the forum, looked at reported issue and pull request(s) related to this step. There doesn’t seem to be any helpful answer because the issue seems unresolved at this point.

I want to move on instead of waiting for the issue to be resolved.
Is it possible? Any suggestion?

I also couldn’t resist looking at the next step.

In retrospect:
I failed to understand that a character class refers to the square brackets.
\d only represents the character that is supposed to be inside the character class.

In that case, the hint message could perhaps be updated to:
Your first capture group should include a character class with \d in it.

In the source code of Step 75 (I won’t post it here), the first capture group makes no sense, since it would allow input such as “1.1.1.1” or even worse — “…”, which is not even a quasi-number.