PDA

View Full Version : calc avge. number from string of ')' separated digits in textarea and show message


matthewb
04-15-2003, 04:58 PM
Hello

I've been looking for a script I can adapt (being pretty lame at javascript) to do the following task and can't find one. So I'm asking you guys; 'cos you're clever an' that :)

I have a textarea which ends up with a lot of content along the lines of:

thing one (5/5)
the other thing (3/5)
XXX (1/5)
summat else maybe (5/5)

I'm looking for a script which will extract the numbers from this text area and do the following to them on a button click (NB: there will only be numbers inside brackets - nowhere else inside the text area):

1) Add each of the individual digits together (in our example we'd get 34).
2) Subtract 'a' number (in the example I want to lose all of the second '5's so I'd minus '20').
3) Divide the total by a number (in this example the number of results - so divide by '4'*.

*in the test it's possible to skip questions so it might be handy if, instead of using a 'static number' it uses a number value equal to the number of close brackets (the reverse parentheses character).

In essence it calculates an average score from some test results. It should calculate the average score based only on the answers completed, which could in theory vary. All of the output takes the form listed above (ie. blah blah blah (X/5)).

The name of the textarea with the results is 'compileddata'.

I'd be *really* good if it could take the average score and do something along the lines of:

if averagescorevalue=1.0-1.5 message='you are a thicky'
if averagescorevalue=1.51-2.0 message='you are still pretty thick'
if averagescorevalue=2.01-2.5 message='you are average'
if averagescorevalue=2.51-5.0 message='OK, smarty-pants, go away'

...where the 'message' would be displayed in a text field (because another script will copy it from there and format it in nicely with the rest of their feedback).

I'm a bit thick, so cut and paste would be brilliant :)

Cheers in advance for any help.

Matthew

beetle
04-15-2003, 05:51 PM
<html>
<head>
<title>Test</title>

<script type="text/javascript">

function getScore( ta )
{
var pairs = ta.value.match( /[0-5]\/5/g );
var pair, i = 0, total = 0, msg = "";
while( pair = pairs[i++] )
{
total += parseInt( pair.match( /\d/ ) );
}
var score = total / --i;
switch( true )
{
case ( score < 1 ) : msg = "Did you even READ the questions?"; break;
case ( score < 1.5 ) : msg = "you are a thicky"; break;
case ( score < 2 ) : msg = "you are still pretty thick"; break;
case ( score < 2.51 ) : msg = "you are average"; break;
case ( score < 5 ) : msg = "OK, smarty-pants, go away"; break;
}
alert( msg );
}

</script>

</head>

<body>

<form name="blah2" action="whatever.ext" method="post">

<textarea name="output" rows="5" cols="70">thing one (5/5)the other thing (3/5)XXX (1/5)summat else maybe (5/5)</textarea>
<input type="button" value="Get Score" onclick="getScore( this.form.output )" />

</form>

</body>
</html>I had a couple different ideas about extracting the numbers. There's always more that one way to skin a cat. The red-code above can be replaced with this var nums = ta.value.match( /[0-5]/g );
var scores = nums.length / 2;
var total = eval( nums.join( "+" ) ) - ( 5 * scores );
var score = total / scores;but, the first solution is better.

matthewb
04-15-2003, 07:18 PM
Hi again.

That’s really *very* groovy. It all works swimmingly :)

Just one slight extra request... I hope I’m not asking too much. I’d really like for the message that currently displays in the alert box to appear in a text field. I’m not sure quite how easy that is to achieve.

I have a text field:

<input type=text name=scorecomment size=65 readonly>

which is in the form named ‘form1’. I really need the answer there so that it’s value can be copied out and formatted in with some other information the test creates.

At the very end, there’s a button which takes the information in about 7 textareas and text fields and formats them all with tabs and whatnot and pops them into a final textarea which is where ‘the final result’ is collected from.

Any chance of an edit on

}
alert( msg );
}

that bit?

Cheers (and thanks again for that last bit – ace!),

Matthew

beetle
04-15-2003, 07:45 PM
Sure, if your input is in the same form, just use this line instead

ta.form.scorecomment.value = msg;

matthewb
04-15-2003, 07:50 PM
Bettle, thank you.

You've made me a very happy man :)

May you have weather as uncharacteristically nice as it is here at the moment... :D

Cheers,

Matthew