Switch(score)

W

warren

I try to create an IQtest for my homepage, and have downloaded a
JavaScript code from another site with permission from the author, I
cant contact the author at his email anymore unfortually, so thats why
I place my question in this forum.

Now to the problem:

On my IQtest http://yalla.dk/projectjs/test.htm ( not finished ) Ive
some different cases, if the person has 0 correct, it will show this
site http://yalla.dk/projectjs/result.html?result=0 - if the person
score 1 correct, it will show this site http://yalla.dk/projectjs/result.html?result=1
and so on. But my problem is when the person score 10 correct (
http://yalla.dk/projectjs/result.html?result=10 ) then the site
display its like 1 correct, this problem only occure after ...result=9
- mayby its because that 10 is a two digis number?

I hope you people out there understand what I mean, and can help me
out of this problem.



var answers=new
Array('D','C','B','D','D','C','C','C','C','C','C','C','C','C','C','C','C','C','C','C');
var query=1;
var age=location.search.substr(5,1);
var tick=480;

function updateAnswer(value) {
setFieldValue(eval('document.forms[0].Item' + query), value, true);
if(tick>0) {
if(query<20) {
query++;
}
doNavigate();
}
}

function doNavigate() {
document.getElementById('testcount').innerHTML=query;
document.getElementById('testimage').src='gf/item' + query + '.JPG';
var curValue=getFieldValue(eval('document.forms[0].Item' + query));
if(curValue=='') {
for(var i=0;i<5;i++) {
document.forms[0].Item.checked=false;
}
}
else {
setFieldValue(document.forms[0].Item, curValue, true);
}
if(query<2) {
document.getElementById('Previous').style.visibility='hidden';
}
else {
document.getElementById('Previous').style.visibility='';
}
if(query>19) {
document.getElementById('Next').style.visibility='hidden';
}
else {
document.getElementById('Next').style.visibility='';
}
}

function countDown() {
tick--;
var minutes=parseInt(tick/60);
var seconds=tick % 60
if(seconds<10) {seconds='0' + seconds}
document.getElementById('timer').innerHTML=minutes + ':' +
seconds;
if (tick == 0) {
alert("Nu har 8 minuter passerat!");
tester();
// document.getElementById('Previous').style.visibility='hidden';
// document.getElementById('Next').style.visibility='hidden';
return;
}
var command="countDown()";
window.setTimeout(command,1000);
}

function tester() {
var score=age;
var frm=document.forms[0];
for(var i=1; i<=answers.length; i++) {
var fld=eval('frm.Item' + i);
if(getFieldValue(fld)==answers[i-1]) {
score++;
}
}

switch(score) {
default:
location.href='result.html?result=0';
break;
case 1:
location.href='result.html?result=1';
break;
case 2:
location.href='result.html?result=2';
break;
case 3:
location.href='result.html?result=3';
break;
case 4:
location.href='result.html?result=4';
break;
case 5:
location.href='result.html?result=5';
break;
case 6:
location.href='result.html?result=6';
break;
case 7:
location.href='result.html?result=7';
break;
case 8:
location.href='result.html?result=8';
break;
case 9:
location.href='result.html?result=9';
break;
case 10:
location.href='result.html?result=10';
break;

}
}

function displayResult() {
var result=location.search.substr(8,1);
document.getElementById('result' + result).style.display='';
}

function isArray(obj) {
if(obj.constructor.toString().indexOf("Array") == -1) {
return false;
}
else {
return true;
}
}

function setFieldValue(field, value, replace) {
if(field.type) {
var fieldType=field.type;
}
else {
var fieldType=field[0].type;
}
switch(fieldType) {
case "text":
case "textarea":
case "password":
case "hidden":
if(replace) {
field.value=value;
}
else {
field.value+=value;
}
case "select-one":
if(isArray(value)) {
value=value[0];
}
for(var i=0; i<field.length; i++) {
if(field.options.text==value || field.options.value==value)
{
field.options.selected=true;
break;
}
}
break;
case "select-multiple" :
var arrFlag=isArray(value);
for(var i=0; i < field.options.length; i++) {
if(field.options.selected && replace) {
field.options.selected=false;
}
if(arrFlag) {
for(var j=0; j<value.length; j++) {
if(field.options.text==value[j] ||
field.options.value==value[j]) {
field.options.selected=true;
}
}
}
else {
if(field.options.text==value || field.options.value==value)
{
field.options.selected=true;
}
}
}
break;
case "button" :
case "reset" :
case "submit" :
return;
case "radio" :
if(isArray(value)) {
value=value[0]
}
for (i = 0; i < field.length; i++) {
if (field.value==value) {
return field.checked=true;
}
}
break;
case "checkbox" :
var arrFlag=isArray(value);
for(i = 0; i < field.length; i++) {
if(field.checked && replace) {
field.checked=false;
}
if(arrFlag) {
for(var j=0; j<value.length; j++) {
if(field.value==value[j]) {
field.checked=true;
break;
}
}
}
else {
if(field.value==value[j]) {
field.checked=true;
break;
}
}
}
default :
var str = "";
for (x in field) {
str += x + "\n";
}
alert("I couldn't figure out what type this field is...\n\n" +
field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
}
}

function getFieldValue(field) {
if(field.type) {
var fieldType=field.type;
}
else {
var fieldType=field[0].type;
}
switch(fieldType) {
case "text":
case "textarea":
case "password":
case "hidden":
return field.value;
case "select-one":
var i=field.selectedIndex;
if (i == -1) {
return "";
}
else {
if(field.options.value=='') {
return field.options.text;
}
else {
return field.options.value;
}
}
case "select-multiple" :
var allChecked = new Array();
for(i = 0; i < field.options.length; i++) {
if(field.options.selected) {
if(field.options.value=='') {
allChecked[allChecked.length]=field.options.text
}
else {
allChecked[allChecked.length]=field.options.value
}
}
}
return allChecked;
case "button" :
case "reset" :
case "submit" :
return "";
case "radio" :
for (i = 0; i < field.length; i++) {
if (field.checked) {
return field.value;
}
}
return "";
case "checkbox" :
var allChecked = new Array();
for(i = 0; i < field.length; i++) {
if(field.checked) {
allChecked[allChecked.length]=field.value;
}
}
return allChecked;
default :
var str = "";
for (x in field) {
str += x + "\n";
}
alert("I couldn't figure out what type this field is...\n\n" +
field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
}
}
 
V

VK

Try:
switch(+score) {
case 1:
location.href='result.html?result=1';
break;
case 2:
location.href='result.html?result=2';
break;
case 3:
location.href='result.html?result=3';
break;
case 4:
location.href='result.html?result=4';
break;
case 5:
location.href='result.html?result=5';
break;
case 6:
location.href='result.html?result=6';
break;
case 7:
location.href='result.html?result=7';
break;
case 8:
location.href='result.html?result=8';
break;
case 9:
location.href='result.html?result=9';
break;
case 10:
location.href='result.html?result=10';
break;
default:
location.href='result.html?result=0';
}
 
P

pr

But my problem is when the person score 10 correct (
http://yalla.dk/projectjs/result.html?result=10 ) then the site
display its like 1 correct, this problem only occure after ...result=9 [...]

function displayResult() {
var result=location.search.substr(8,1);

Looks like you're retrieving only the character at position 8 in the
query string. Try

var result = window.location.search.substr(8);

to read the string from character 8 onwards.
document.getElementById('result' + result).style.display='';
}
[...]
 
W

warren

(e-mail address removed) wrote:

[...]
But my problem is when the person score 10 correct (
http://yalla.dk/projectjs/result.html?result=10) then the site
display its like 1 correct, this problem only occure after ...result=9 [...]

function displayResult() {
   var result=location.search.substr(8,1);

Looks like you're retrieving only the character at position 8 in the
query string. Try

   var result = window.location.search.substr(8);

to read the string from character 8 onwards.
   document.getElementById('result' + result).style.display='';
}

[...]

Thank you so much, it works perfect now. ;-)
 
T

Thomas 'PointedEars' Lahn

VK said:
Try:
switch(+score) {
case 1:
location.href='result.html?result=1';
break;
case 2:
location.href='result.html?result=2';
break;
case 3:
location.href='result.html?result=3';
break;
case 4:
location.href='result.html?result=4';
break;
case 5:
location.href='result.html?result=5';
break;
case 6:
location.href='result.html?result=6';
break;
case 7:
location.href='result.html?result=7';
break;
case 8:
location.href='result.html?result=8';
break;
case 9:
location.href='result.html?result=9';
break;
case 10:
location.href='result.html?result=10';
break;
default:
location.href='result.html?result=0';
}

Only you could come up with that kind of inefficient, error-prone,
unmaintainable script-kiddie nonsense.

score = parseInt(score, 10);
if (!isNaN(score) && score >= 0 && score <= 10)
{
window.location = 'result.html?result=' + score;
}


PointedEars
 
K

kangax

Try:
switch(+score) {
case 1:
location.href='result.html?result=1';
break;
case 2:
location.href='result.html?result=2';
break;
case 3:
location.href='result.html?result=3';
break;
case 4:
location.href='result.html?result=4';
break;
case 5:
location.href='result.html?result=5';
break;
case 6:
location.href='result.html?result=6';
break;
case 7:
location.href='result.html?result=7';
break;
case 8:
location.href='result.html?result=8';
break;
case 9:
location.href='result.html?result=9';
break;
case 10:
location.href='result.html?result=10';
break;
default:
location.href='result.html?result=0';
}

Are you serious?
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Try: if (obj instanceof Array)

if (obj.constructor == Array)

is more compatible and sufficient since you cannot derive from
Array.prototype and keep your object working like an Array object.

Please trim your quotes.


PointedEars
 
L

Lasse Reichstein Nielsen

function tester() {
var score=age;
var frm=document.forms[0];
for(var i=1; i<=answers.length; i++) {
var fld=eval('frm.Item' + i);
if(getFieldValue(fld)==answers[i-1]) {
score++;
}
}

switch(score) {
default:
location.href='result.html?result=0';
break; .....
case 10:
location.href='result.html?result=10';
break;


Much shorter:
}
}

function displayResult() {
var result=location.search.substr(8,1);

Only one character? That fails for "result=10" and gives the same
result as "result=1". This is where your bug is.
document.getElementById('result' + result).style.display='';
}

function isArray(obj) {
if(obj.constructor.toString().indexOf("Array") == -1) {

Try: if (obj instanceof Array)
return false;
}
else {
return true;
}

and booleans are values too. You don't need an if, just:
return (obj instanceof Array);
}

function setFieldValue(field, value, replace) {
if(field.type) {
var fieldType=field.type;
}
else {
var fieldType=field[0].type;
}
switch(fieldType) {
case "text":
case "textarea":
case "password":
case "hidden":
if(replace) {
field.value=value;
}
else {
field.value+=value;
}
case "select-one":
if(isArray(value)) {
value=value[0];
}
for(var i=0; i<field.length; i++) {
if(field.options.text==value || field.options.value==value)
{
field.options.selected=true;
break;
}
}
break;
case "select-multiple" :
var arrFlag=isArray(value);
for(var i=0; i < field.options.length; i++) {
if(field.options.selected && replace) {
field.options.selected=false;
}
if(arrFlag) {
for(var j=0; j<value.length; j++) {
if(field.options.text==value[j] ||
field.options.value==value[j]) {
field.options.selected=true;
}
}
}
else {
if(field.options.text==value || field.options.value==value)
{
field.options.selected=true;
}
}
}
break;
case "button" :
case "reset" :
case "submit" :
return;
case "radio" :
if(isArray(value)) {
value=value[0]
}
for (i = 0; i < field.length; i++) {
if (field.value==value) {
return field.checked=true;
}
}
break;
case "checkbox" :
var arrFlag=isArray(value);
for(i = 0; i < field.length; i++) {
if(field.checked && replace) {
field.checked=false;
}
if(arrFlag) {
for(var j=0; j<value.length; j++) {
if(field.value==value[j]) {
field.checked=true;
break;
}
}
}
else {
if(field.value==value[j]) {
field.checked=true;
break;
}
}
}
default :
var str = "";
for (x in field) {
str += x + "\n";
}
alert("I couldn't figure out what type this field is...\n\n" +
field.name + ": ???\n\n\n" + str + "\n\nlength = " + field.length);
}
}

function getFieldValue(field) {
if(field.type) {
var fieldType=field.type;
}
else {
var fieldType=field[0].type;
}
switch(fieldType) {
case "text":
case "textarea":
case "password":
case "hidden":
return field.value;
case "select-one":
var i=field.selectedIndex;
if (i == -1) {
return "";
}
else {
if(field.options.value=='') {
return field.options.text;
}
else {
return field.options.value;
}
}
case "select-multiple" :
var allChecked = new Array();
for(i = 0; i < field.options.length; i++) {
if(field.options.selected) {
if(field.options.value=='') {
allChecked[allChecked.length]=field.options.text
}
else {
allChecked[allChecked.length]=field.options.value
}
}
}
return allChecked;
case "button" :
case "reset" :
case "submit" :
return "";
case "radio" :
for (i = 0; i < field.length; i++) {
if (field.checked) {
return field.value;
}
}
return "";
case "checkbox" :
var allChecked = new Array();
for(i = 0; i < field.length; i++) {
if(field.checked) {
allChecked[allChecked.length]=field.value;
}
}
return allChecked;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top