Page 1 of 1

[HELP] Why Prompt answer not always working

Posted: Tue Aug 13, 2024 9:01 pm
by Epton
Hey Guys, I'm using Prompt and give it a Variable 'Answer' then I use an IF with
Answer == Answer.trim().match(/^No Miss|No Ma'am|no|no Sir|No *goddess$/i)
But for some reason when I type 'no goddess' it doesn't work and if I type 'no' it does work :-/
Why it doesn't validate the 'goddess' after each string I've specified :\'-( :rant:

Re: [HELP] Why Prompt answer not always working

Posted: Wed Aug 14, 2024 7:32 am
by Thamrill
Epton wrote: Tue Aug 13, 2024 9:01 pm Hey Guys, I'm using Prompt and give it a Variable 'Answer' then I use an IF with
Answer == Answer.trim().match(/^No Miss|No Ma'am|no|no Sir|No *goddess$/i)
But for some reason when I type 'no goddess' it doesn't work and if I type 'no' it does work :-/
Why it doesn't validate the 'goddess' after each string I've specified :\'-( :rant:
Tried it out in javascript tester, I think the issue is that if you put "No goddess" (and the same is true for "No Sir"), it matches the simple "no" options, which then fails the comparison check; try putting the "no" option last, should work

Re: [HELP] Why Prompt answer not always working

Posted: Wed Aug 14, 2024 6:02 pm
by Epton
Thamrill wrote: Wed Aug 14, 2024 7:32 am
Epton wrote: Tue Aug 13, 2024 9:01 pm Hey Guys, I'm using Prompt and give it a Variable 'Answer' then I use an IF with
Answer == Answer.trim().match(/^No Miss|No Ma'am|no|no Sir|No *goddess$/i)
But for some reason when I type 'no goddess' it doesn't work and if I type 'no' it does work :-/
Why it doesn't validate the 'goddess' after each string I've specified :\'-( :rant:
Tried it out in javascript tester, I think the issue is that if you put "No goddess" (and the same is true for "No Sir"), it matches the simple "no" options, which then fails the comparison check; try putting the "no" option last, should work
Maybe it was cuz of the 2 no, but I deleted the middle one and it weirdly worked, but I noticed something very wrong with what I had written so I changed it lol. Thanks for the answer, it clarified my mind.

Re: [HELP] Why Prompt answer not always working

Posted: Wed Aug 14, 2024 7:44 pm
by kerkersklave
Do you really put this as a condition?

Code: Select all

Answer == Answer.trim().match(/^No Miss|No Ma'am|no|no Sir|No *goddess$/i)
You are comparing "Answer" to the result of triming and matching. match returns null if there is no match, so it should be:

Code: Select all

null != Answer.trim().match(/^No Miss|No Ma'am|no|no Sir|No *goddess$/i)
The reason you get true for "no" is, that | has priority over ^ and $, so your expression matches something that starts with "No Miss", contains "No Ma'am", "no" or "no Sir" or ends with "No goddess". If you then compare Answer to that match result it will convert the match result for whatever has been matched.

For "no" that works, even for "No" it wont work. "No" is also matched for "No Goddess" because Javascript takes the first match, which is "no" as you are not requiring it to match to the end.

What you actually want is this:

Code: Select all

null != Answer.trim().match(/^no( +(Miss|Ma'am|Sir|goddess))?$/i)
And because javascript is actually converting most values to true and null and some others to false, you could directly use:

Code: Select all

Answer.trim().match(/^no( +(Miss|Ma'am|Sir|*goddess))?$/i)
as a condition for if.

If you press f12 in Chrome or Firefox you will get a javascript console, where you can evaluate expressions and see what you get as a result.