Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 calculation from array doesn't add up

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!
Before posting, make sure you have read this topic!

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
   

T O P I C    R E V I E W
ILLHILL Posted - 05 June 2010 : 09:29:16
Hi everybody,
I'm currently writing a piece of code to compare the answers of 10 multiple questions with the answers. The optional answers to the questions have values of w, x, y and z. These then get compared to an array, for example:

x,x,x,x,x,x,x,x,x,x

On the quiz page the values get send to the results page.

However, when all 10 answers are correct, the outcome is 9 out of 10 points. The answer from the first question is ignored. Here's the code:

MyArray2 = Split(answer_string, ",", -1, 1)

answer1 = MyArray2(0)
answer2 = MyArray2(1)
answer3 = MyArray2(2)
answer4 = MyArray2(3)
answer5 = MyArray2(4)
answer6 = MyArray2(5)
answer7 = MyArray2(6)
answer8 = MyArray2(7)
answer9 = MyArray2(8)
answer10 = MyArray2(9)

quizscore_final = 0

if q1 = answer1 then
quizscore_final = quizscore_final + 1
end if

if q2 = answer2 then
quizscore_final = quizscore_final + 1
end if

if q3 = answer3 then
quizscore_final = quizscore_final + 1
end if

if q4 = answer4 then
quizscore_final = quizscore_final + 1
end if

if q5 = answer5 then
quizscore_final = quizscore_final + 1
end if

if q6 = answer6 then
quizscore_final = quizscore_final + 1
end if

if q7 = answer7 then
quizscore_final = quizscore_final + 1
end if

if q8 = answer8 then
quizscore_final = quizscore_final + 1
end if

if q9 = answer9 then
quizscore_final = quizscore_final + 1
end if

if q10 = answer10 then
quizscore_final = quizscore_final + 1
end if

I'm not that familiar with arrays, but is there any reason why the first question answer is not correctly calculated?

I have tried "response.write" to display the selected answers as well as the given answers and these get listed correctly. However, the score result has a maximum of 9 max instead of 10.

Anybody has any suggestions?

With kind regards,

Dominic

****Edit****
I noticed that the in predetermined answers that get pulled from the array, a space is added before the variable of answer1. This seems to cause the issue, as "X" is not equal to " X".

answer1 = X
answer2 =X
answer3 =X
answer4 =X

Why is this space added when the array is split?
6   L A T E S T    R E P L I E S    (Newest First)
Carefree Posted - 05 June 2010 : 12:01:29
Well, if you know the problem is with answer 1 having a leading space, it's a simple thing to resolve.

quote:

if q1 = answer1 then



Change to:

if q1 = right(answer1,1) then

or, if the answer can have more than one character:

if q1 = mid(answer1,2) then
bobdsw Posted - 05 June 2010 : 10:58:28
When you look at myarray2, before you split it, does the space exist?

Trim should work. I would make sure you're running it on both the q and the answer for each. You may also want to try running lcase or ucase on both as well, just in case.
ILLHILL Posted - 05 June 2010 : 10:26:58
Hi HuwR, thanks for the reply.

Unfortunately Trim and Replace (replacing " " with "") have not worked yet for either the string as a whole or the variable.
I will continue trying though.

Thanks again.

Greets, Dominic
HuwR Posted - 05 June 2010 : 10:04:46
you could also try trimming answer_string before splitting into an array

MyArray2 = Split(Trim(answer_string), ",")
ILLHILL Posted - 05 June 2010 : 10:01:20
Hi bobdsw,
Thanks for your reply. It seems you posted it at the moment I was editing my post.
A space is added to the first answer when the array is split. I have no idea why this space is added, but "X" is logically not equal to " X", so that seems to be why the first question is answered incorrect according to the script.

Any idea why this space is added to the array?

array string = "X,X,X,X,X,X,X,X,X,X," there's no space in front of the first X.

Greets & thanks, Dominic
bobdsw Posted - 05 June 2010 : 09:56:43
Try remming out the If statement (q1 = answer1) for the first element in the array and see if it's added.

If it is then the problem is the If statement itself and you'll need to check either the values of q1 and answer1 and verify they are equal.

Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 1.19 seconds. Powered By: Snitz Forums 2000 Version 3.4.07