PDA

View Full Version : Regular Expression and or Array(for input and output)of a text string.


mike B
09-21-2002, 08:24 PM
example:the var (swirling leaves)=answer.I would like to be able to return an (alert) as true or correct! even though the (object) or noun(this case)leaves,did not have adjective or the other true part or description of the statement(swirling).I would like the output to state (true/correct)on the leaf answer and substitute or show the complete answer(swirling leaves)

joh6nn
09-22-2002, 12:06 AM
i have no idea what you just said. can you explain that again?

umm
09-22-2002, 02:07 AM
I agree, that was not a clear description of the problem. However, after a few goes I think I got the gist of what you want. Try this as a statring point.

<html>
<head>
<title>Example</title>
<script language="javascript" type="text/javascript">
<!-- ;
function answerTest(f){
if(f.txt01.value!="ing")
alert("Sorry, the correct answer was:\n\nSwirling Leaves");
else
alert("Swirling Leaves was Correct!");
}
// -->
</script>
</head>
<body>
<form>
<h4>Please Complete the Phrase</h4>
The swirl<input type="text" name="txt01" size="5"/> leaves made no sound<br>
<input type="button" value="Answer" onclick="answerTest(this.form)"/>
</form>
</body>
</html>

adios
09-22-2002, 02:24 AM
Sounds like homework to me. Whatever.

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var answers = Object;
answers['leaf_answer'] = {full: 'swirling leaves' , req: 'leaves'};
answers['truck_answer'] = {full: 'a falling truck' , req: 'truck'};

function grade(f) {
var field, ans, ansRE, req_ans, req_ansRE, counter = 1;
for (which in answers) {
ans = answers[which].full;
ansRE = new RegExp('^\\s*' + ans + '\\s*$');
req_ansRE = new RegExp('^\\s*' + answers[which].req + '\\s*$');
field = f[which];
if (ansRE.test(field.value)) alert('Answer ' + counter++ + ' is correct!');
else if (req_ansRE.test(field.value)) {
alert('Close! The full answer to question ' + counter++ + ' was "' + ans + '."');
field.value = ans;
}
else alert('Answer ' + counter++ + ' is incorrect.');
}
}

</script>
</head>
<body>
<form>
<h4>What might take your eye out on a windy day?</h4>
<input type="text" name="leaf_answer"><br>
<h4>What might take your eye out on a calm day?</h4>
<input type="text" name="truck_answer"><br><br>
<input type="button" value="GRADE" onclick="grade(this.form)">
</form>
</body>
</html>

mike B
09-22-2002, 09:14 PM
The following code I wrote works just fine.But I would like to improve on it and this is where I was not so clear.It is sort of a word game,view the(code).Again the full correct answer would be,example: var (swirling leaves)=answer.I would like to leave some flex,so the return or answer text string could be for instance(a leaf or a pile of leaves)in which case the object(leaves)is the correct answer or operative word and would show in the display as such (leaf or leaves) and ignore the words (a pile of )" " .

mike B
09-22-2002, 09:24 PM
Again!! thanks to all who helped and are helping with this issue.

RadarBob
09-22-2002, 09:32 PM
A facinating problem. But sounds like artificial intelligence to me!

mike B
09-22-2002, 11:51 PM
sorry! again I meant to say,post the input as (correct) as long as the words or operative word or text string input if you like(leaf or leaves)is found within the input string via the user display.NOT!! IGNORE!!(the user is trying to guess the subject!!)so what ever text was entered,would display correct as long as the var=answer is present in the string.(I.E.a wind blown pile of leaves?)or (a wind blown leaf?)either case in the response the word (leaf or leaves was in the answer string)and would be displayed as true or correct via highlighted text along with the other input( a wind blown pile of " ") or ( the wind blew a " ") hope this along with sample code clears up this issue. Again thanks all for your time and consideration..