I find this challenge's instructions pretty confusing. I made the hard part bold.
The last challenge used the plus + sign to look for characters that occur one or more times. There's also an option that matches characters that occur zero or more times.
The character to do this is the asterisk or star: *.
let soccerWord = "gooooooooal!"; let gPhrase = "gut feeling"; let oPhrase = "over the moon"; let goRegex = /go*/; soccerWord.match(goRegex); // Returns ["goooooooo"] gPhrase.match(goRegex); // Returns ["g"] oPhrase.match(goRegex); // Returns null
_Create a regex chewieRegex that uses the * character to match all the upper and lower"a" characters in chewieQuote._ Your regex does not need flags, and it should not match any of the other quotes.
somehow this worked:
let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
let chewieRegex = /Aa*/; // Change this line
let result = chewieQuote.match(chewieRegex);
Here are tests:
4th test is especially confusing. there are lowercase 'a', why should I _not_(added after edit) match them?
The fourth test case checks if your regexp doesn't match the string. The regexp /Aa*/
matches with only an 'A' followed by zero or more 'a's.
I feel the instructions are pretty clear.
Yeah, I know why /Aa/ works, but it isn't clear why you should write /Aa/ from instructions given.
Nowhere in the challenge does it say that the first character of the string must be A
. I think you're meant to write [aA]*
, but I might be wrong?
@jooji-san yes, I believe I might have written these or at the very least contributed them, so as others have pointed out, that fourth test is there so that your regex is designed someone smartly not to just look for lowercase characters. Because the thing with regexes is that if you don't design/write them carefully, you may match things that you didn't intend to, and thus should rewrite them. So if you did just /a*/
, then you would match something in that quote, which for this challenge's purpose, you don't want.
I hope that clears things up. Given my explanation, do you still think there should be clarification as to why that fourth test should be added? We can make those changes in the directions to suggest you don't want to match things you didn't intend if that makes it more clear.
@joker314 you can use character classes, but I think it is left ambiguous for people to interpret how you want, as lots of things in programming have different answers to the same problem.
See #17632 for a discussion and some history.
Hi @lynxlynxlynx, sorry you're having trouble with this challenge. It was a long time ago, but I think I remember the original challenge having some issues with introducing both the ^
and *
characters at the same time. I thought that was too much, so rewrote it to just introduce the *
character. Also in the original challenge, the user didn't have to use the *
character at all since all the movie titles have "Star Wars" in them--just let starWarsRegex = /starWars/;
or let starWarsRegex = /Star Wars/;
would pass.
So that's why I went with tests that would require the user to use Aa*
, or an uppercase "A" followed by lowercase "a" characters. At least that was the intention.
I see that one solution you tried was [aA]*
. That character set does match Aaaaaaaaaaaaaaaargh
, but also matches any lowercase or uppercase A's in the other strings. That regex expression could also return empty matches or match infinitely in some cases.
All that said, please let us know if you have suggestions to improve this challenge description or tests! Like @erictleung said, we could always clarify things and hopefully not cause more confusion in the future.
I proposed a solution in the older bug. "Aa*" just doesn't fit the instructions well.
A simple expression with an asterisk will always be problematic when it comes to matching the empty string. So it's silly to try to force users too use it. "Aa*" is just hacky one-or-more emulation to get around the problem.
Another solution would be to introduce asterisk after the search-and-replace challenge, which is currently non-regexy anyway #17645. The last, trim, challenge has a perfect use in mind and could easily be presented earlier:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/remove-whitespace-from-start-and-end
I don't think the Instructions state that the initial character in the string will be a capital letter. Solutions should solve the general problem, not the test cases -- otherwise people would write a regex of the form /Aaaaaaaaaaaaaaaargh/
If we want users to type Aa*
then we should add tests like A
and Aaaaaa
, and clarify that the sequence must begin with a capital letter.
@erictleung My main complaint is the general problem.
Create a regex chewieRegex that uses the * character to match all the upper and lower"a" characters in chewieQuote
it doesn't explain that upper and lower case 'a' characters should be together side-by-side in one word. Based on this problem regex should also work on these strings: 'A', 'a', 'A a' and etc.
@lynxlynxlynx, right, the asterisk can be really tricky. But I think the problem with using that specific character set with the asterisk outside that can lead to matching empty strings or match infinitely. And while you bring up a good point about the search-and-replace challenge not being too regexy right now, I think the order of this challenge is fine. Having it directly after the "Match Characters that Occur One or More Times" challenge seems to make sense. What was the solution you proposed for the earlier version of the challenge?
@joker314 and @jooji-san, clearly the description and lack of tests are causing some confusion. In the "Ignore Case While Matching" earlier in the unit there was a brief explanation about case differences, but this challenge could be a good opportunity to remind users about that. Would any of you be willing to improve the description and write some additional tests? Like @joker314 wrote, adding some tests using upper and lowercase a
characters of different length would make things more clear. There's already a test to make sure the users use the *
character, so you shouldn't need to worry about anyone cheesing the challenge by matching the exact string.
I linked the bug, just look at it. As for matching nothing or "infinitely", that's only somewhat of a test problem, since the feature works that way by design (and we're not testing against G-sized strings here).
@scissorsneedfoodtoo This bug has nothing to do with the asterisk being tricky. It has to do with the fact that _the instructions are just plain wrong._
The instructions say:
Create a regex chewieRegex that uses the * character to match all the upper and lower"a" characters in chewieQuote. Your regex does not need flags, and it should not match any of the other quotes.
This means a RegEx like /[Aa]*/g
What the instructions should say is:
Create a regex chewieRegex that uses the * character to match all the upper and lower"a" characters in chewieQuote that have an initial "A". Your regex does not need flags, and it should not match any of the other quotes.
Now we get to the desired RegEx of /A[Aa]*/g
@JESii, that description seems really close to solving a lot of the problems that many people have with it. Although character classes were introduced in an earlier lesson, it's not necessary here, so we could include that in the description, too. This lesson is really about the *
character matching one thing zero or more times, so the description could definitely be more clear about that. Maybe it could read something like:
"Create a regex <code>chewieRegex</code> that uses the <code>*</code> character to match the initial uppercase <code>\"A\"</code> character and all of the lowercase <code>\"a\"</code> characters immediately after it in <code>chewieQuote</code>. Your regex does not need flags or character classes."
Then we could introduce more tests like @joker314 mentioned above: A
and Aaaa
which should both pass, and aaaaa
, which should fail, and so on.
Would you be interested in updating this challenge? If so, the file that needs to be edited is here.
I agree with @scissorsneedfoodtoo, please update the question as stated from his comment.
I agree. This is an ambiguous challenge.
I'll file a PR soon1 to resolve the issue of wording.
1 - 0 to 2 days
@joker314 hey are you still working on this?
Nope
Most helpful comment
@scissorsneedfoodtoo This bug has nothing to do with the asterisk being tricky. It has to do with the fact that _the instructions are just plain wrong._
The instructions say:
This means a RegEx like
/[Aa]*/g
What the instructions should say is:
Now we get to the desired RegEx of
/A[Aa]*/g