Page 1 of 1

Words among a field

Posted: Tue Jan 26, 2016 11:28 am
by pjfry
Hi there,

I try to program a learning app. There are 60 questions with text answers. This could look like this:
"What are the colors?"

Answers is

"Blue and red"
So I have an label with the question and a text field for the answer. So the person answers the quest and I like the app to look at the textfield if it conatins the words "blue" and red".

So I did this (which is working):

Code: Select all

   
put "red" into theAnswer
   if theAnswer is among the words of field myField
   then answer "Great"
   else answer "False"
Put this only work with one word. I like to check is there are both words "red" and "blue" in the answer text field. But I d not know how. There could be up to 20 words I have to check. Some with AND and some with OR operators? Is there a way to achieve this?

Thanks for your help

Benny

Re: Words among a field

Posted: Tue Jan 26, 2016 11:45 am
by Thierry
Hi Benny,

As a start, something like that?

Code: Select all

function isRightAnswer mySolution,  userAnswer
   repeat for each word W in mysolution
      -- every word W must be in the user's answer
      if W is not among the words of userAnswer then return false
   end repeat
   return true
end isRightAnswer
and to test it:

Code: Select all

on mouseUp
   put "A B C" into mySolution
   put "B C" into userAnswer
   
   if isRightAnswer( mySolution,  userAnswer) then
      answer "Great!"
   else
      answer "Too bad :("
   end if
end mouseUp
HTH,

Thierry

Re: Words among a field

Posted: Tue Jan 26, 2016 12:06 pm
by Dixie

Code: Select all

on mouseUp
   if "red" is among the words of line 1 of fld 1 AND "blue" is among the words of line 1 of fld 1 then
      beep
   end if
end mouseUp

Re: Words among a field

Posted: Tue Jan 26, 2016 1:38 pm
by pjfry
Hi there,

thank you both for your fast help. One more Question :)

Thierry your code look fine to me and works also. But it looks like "is among" is not right for my use case.

Example:
If mySolution is balls and the user write ball its wrong. So I thought I usw your code snippet with contains .. but this doesnt work. I used it this way:

Code: Select all

function isRightAnswer mySolution,  userAnswer
   repeat for each word W in mysolution
      -- every word W must be in the user's answer
      if userAnswer contains not W then return false
   end repeat
   return true
end isRightAnswer
but now all is a valid answer and I do not know why :)

Thanks Benny

Re: Words among a field

Posted: Tue Jan 26, 2016 2:01 pm
by zaxos

Code: Select all

function isRightAnswer mySolution,  userAnswer
   repeat for each word W in mysolution
      -- every word W must be in the user's answer
      if userAnswer contains W then
        -- do nothing
        else
        return false
      end if
   end repeat
   return true
end isRightAnswer
If all words are in the answer the function will return true when repeat ends, if not it will return false and exit repeat. I dont think "contains not" i supposed to work, that why you get those results.

Re: Words among a field

Posted: Tue Jan 26, 2016 3:36 pm
by Thierry
pjfry wrote: Thierry your code look fine to me and works also.
But it looks like "is among" is not right for my use case.
?

I tried this case:

Code: Select all

   put "A B Cs" into mySolution
   put "B A C" into userAnswer
which returns false, which is fine.
Or what did I miss?


Otherwise check in the dictionary:

Code: Select all

set the wholeMatches to true

and this line is not right ( Hint: not at the wrong place ) :

Code: Select all

if userAnswer contains not W then return false

HTH,

Thierry

Re: Words among a field

Posted: Tue Jan 26, 2016 6:14 pm
by quailcreek
This might work for you.

Code: Select all

on mouseUp
   if fld "thequestion" contains "Red" AND fld "thequestion" contains "Green" then 
      answer "True"
   else 
      answer "False"
   end if
end mouseUp

Re: Words among a field

Posted: Tue Jan 26, 2016 7:10 pm
by jacque
As Thierry said, you need:

Code: Select all

if not (userAnswer contains W)  then return false
Be aware that using this syntax, "ball" will match eyeball, ballistic, balloons, etc.