Click to See Complete Forum and Search --> : Calculating scores based on other scripts


cidlockie
12-04-2003, 04:54 AM
I honestly couldn't think of a descriptive enough subject line so I'm sorry if that's a bit pitiful for this, but I hope someone can help.

Brief as I can: I have a site which basically shows snippets of dialogue from various movies, and asks readers to guess the film in an input box. A script checks the guess and if it's right it changes a standard image to the film's actual poster (thus showing the reader they've got the answer right).

Here's one of the individual films' sections...



<P align="center">"Give me that pistol, Mister."
<BR>"Huh?"
<BR>"I says give me over your pistol."
<BR>"Well, I ain't drunk."
<BR>"Ordinance says you got to turn in your firearms to the county office, day or night. I guess you didn't see the sign with the weather and all."
<BR>"No. But I ain't...I ain't armed."
<BR>"What about your friends upstairs? They got any pistols?"
<BR>"I don't know. No, I guess...No. They ain't armed either."
<BR>"Uh-huh. Spilled your whiskey?"
<BR>"Look, I said-"
<BR>"What's your name?"
<BR>"William Hendershot."
<BR>"Well, Mister William Hendershot, what if I was to say you was a no good son of a ***** and a liar?"</P>
<BR CLEAR=left>

<script type="text/javascript">

var pic62guess = "UNFORGIVEN"
function checkbj(){
frm=document.f62;
wk=frm.tbj.value.split('\r');
ln = wk.length;
for (var i=0; i<ln; i++) {
wk[i]=wk[i].replace(/ /g, '');
}
frm.tbj.value=wk.join('\r');
frm.tbj.value=frm.tbj.value.toUpperCase()
if(frm.tbj.value==pic62guess){
document.images["picbj"].src = pic62guess + ".jpg"
}
else {
alert("Incorrect. Please try again.");
frm.tbj.value="";
frm.tbj.focus();
}
}
</script>


<P align="center"><form name="f62" onSubmit="return false">
Save Heather - name the film<br>
<img name="picbj" src="heather.jpg"><br>
<input type="text" name="tbj"><br>
<input type="button" value="Go" onclick="checkbj()">
</form></P>



It's reasonably good fun, I'm told...but what it lacks is a way to calculate scores.

So, what I need to do is somehow find a script which adds up the number of correct guesses on a page and displays the total somewhere, at the bottom or the top or in a box when you click 'Calculate score' or anything like that.

I don't know whether I would be best to add up the number of images which have switched from 'heather.jpg' to the correct one, or to use some function to calculate the number of times the check function has been correctly employed on a page. And to be honest, I'd be pretty unsure where to start with either.

If anyone has a clue where I might begin with this it would be a great help, and just in case it helps in any way (and please do delete this if it's breaking protocol somehow) the address of the site is http://www.geocities.com/cidlockie/films/filmlist.htm

Thanks very much,
Chris.

clairec666
12-04-2003, 05:22 AM
I haven't read your script all the way through, so I'm not sure this is entirely relevent............ it should help, though:

Create an array whose length is the same as the number of questions. All the items in the array should be set to 'false'.

When somebody guesses correctly (you have already created a function to test this), as well as the picture changing, arrayitem[i] should be set to 'true'.

For the 'add up scores' part:
Run a 'for' loop to test how many of the array items are 'true'. For each time that arrayitem[i] == true, add 1 tot var totalscore, the alert(totalscore)

I'll put up a proper script in a minute, but this should help in the meantime.

cidlockie
12-04-2003, 05:31 AM
Ah, thanks very much, yes I see what you mean: a correct guess switches the relevant arrayitem to true, based on the individual ID of the film (62 or 'bj' in this case), and then add up the number of trues to get the score.

I'll confess, I had someone else 'help' me with the original code (ok, they did it for me..) so I'll be weeing in the wind if I try it, but I guess it's a good time to try and learn to do some java for myself!

clairec666
12-04-2003, 05:35 AM
:) Are you sorted with it now? Hope it works!!

cidlockie
12-04-2003, 05:37 AM
I think.....holds breath......I'll have a bash at it and see what happens. I'll let you know if it works out, my first ever attempt at doing it from scratch.

Or rather, I'll beg for more help if it doesn't!

Thanks very much clairec666,
Chris.

clairec666
12-04-2003, 05:39 AM
:D I guess if it's not working, I'll se you on here in a while tearing your hair out and cursing me

cidlockie
12-04-2003, 06:55 AM
I'm baaaack......

Ok, I put an array like this at the start of the BODY:


var scoreArray = new Array(25);
scoreArray[0] = 0;
scoreArray[1] = 0;
scoreArray[2] = 0;


...down to scoreArray[24], as there are 25 items on the page.

I then added a bit to change the array value if you get it right:


if(frm.ta.value==pic1guess){
document.images["pica"].src = pic1guess + ".jpg";
scoreArray[0] = 1
}


...into each of the 25 separate sections.

Then at the end of the body, I whacked in this, as best I could:


<script type="text/javascript">

function SumScore(scoreArray)
{
var sum = 0;
for (var i=0; i<scoreArray.length; i++)
sum = sum + scoreArray[i];
return sum;
}

</script>

<P align="center"><form name="score" onSubmit="return false">
<input type="button" value="Calculate your score" onclick="checkscore()">
</form></P>


Now, I'm pretty sure I've made an absolute mess of it but I can't tell how! I had tried it another way before and it wasn't returning the 'Object expected' error it is now, but it was perennially returning the score as zero so I scrapped it and did it this way.

If you're still there clairec666, could you give me a couple of pointers?

By the way, does there need to be some kind of alert thing to make the score open in a box? I couldn't be more confused!

Thanks again,
Chris.

clairec666
12-04-2003, 06:58 AM
There's a bit you need to change in the last section.........

<script type="text/javascript">

function SumScore(scoreArray)
{
var sum = 0;
for (var i=0; i<scoreArray.length; i++)
sum++;
window.alert(sum);
}

</script>

I've changed the 'adding' bit slightly, and the window.alert() bit will make the score come up in a popup box.

I'll just have a look at the rest of it.

clairec666
12-04-2003, 07:00 AM
Nope - scrap that!! Sorry, I'm directing you the wrong way.....

<script type="text/javascript">

function SumScore()
{
var sum = 0;
for (var i=0; i<scoreArray.length; i++)
if(scoreArray[i]!=0) sum++;
window.alert(sum);
}

</script>

cidlockie
12-04-2003, 07:09 AM
Hmmm, it's still giving me an 'Object expected' - it says line 1183, and although obviously that doesn't help, it's definitely somewhere in that bit.

Were the other bits right? I mean, putting the array stuff in at the top 25 times, and then adding the 'scoreArray[0] = 1 ' type thing into each individual section?

I know you said I should do ture and false, but I thought using 0 and 1 and then a sum thing at the bottom would be easier. I bet that means I've made a total balls of it!

clairec666
12-04-2003, 07:12 AM
The 0 and 1 thing is fine, works just as well. Have you got the whole script so I can try it out? I'll be disappearing in a few minutes, so if I'm not around and you want to ask more, my e-mail is clairec666@hotmail.com