Words among a field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
pjfry
Posts: 8
Joined: Mon Jun 16, 2014 10:15 am

Words among a field

Post by pjfry » Tue Jan 26, 2016 11:28 am

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Words among a field

Post by Thierry » Tue Jan 26, 2016 11:45 am

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Words among a field

Post by Dixie » Tue Jan 26, 2016 12:06 pm

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

pjfry
Posts: 8
Joined: Mon Jun 16, 2014 10:15 am

Re: Words among a field

Post by pjfry » Tue Jan 26, 2016 1:38 pm

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

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: Words among a field

Post by zaxos » Tue Jan 26, 2016 2:01 pm

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.
Knowledge is meant to be shared.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Words among a field

Post by Thierry » Tue Jan 26, 2016 3:36 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Words among a field

Post by quailcreek » Tue Jan 26, 2016 6:14 pm

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
Tom
MacBook Pro OS Mojave 10.14

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Words among a field

Post by jacque » Tue Jan 26, 2016 7:10 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply