1

How can i compare values for same index for each of these input text and select control array ?

var qtype_array = new Array();
$('select[name="qtype[]"]').each(function(){
    qtype_array.push($(this).val());
}); 

var true_ans_array = new Array();
$('input[name="true_ans[]"]').each(function(){
    true_ans_array.push($(this).val().toUpperCase());
});

ARRAY CONTENTS :

qtype_array - ["MULTIPLE", "SINGLE","DESC"]
true_ans_array - ["AC", "A", ""] 

I want to check that if in qtype_array at index 0 if the text is "MULTIPLE" then at the same index 0 of true_ans_array the text should be minimum 2 characters and if the text in qtype_array is "SINGLE" the length of text contents in true_ans_array at same index should be 1 , and if in qtype_array if the text is "DESC" the length of text contents in true_ans_array should be 0.

I am developing a Online Exam System.

I am badly stuck and cant think of the solution.....

Sandy505
  • 888
  • 3
  • 15
  • 26

2 Answers2

0

See this fiddle, and here's the explanation:

Assume you've gathered all the info from the page, you should have both arrays qtype_array and true_ans_array already populated with data.

Like this:

var true_ans_array = new Array();
$("input[name='true_ans[]']").each(function(){
    true_ans_array.push(this.value);
});

var qtype_array = new Array();
$('select[name="qtype[]"]').each(function(){
    qtype_array.push($(this).val());
});

now both arrays have the elements within. Next is to go through all elements. I'm assuming that there's the same amount of elements for both arrays.

for (i=0; i < qtype_array.length; i++) {

switch (qtype_array[i]) {
    case "MULTIPLE": if (true_ans_array[i].length >= 2) 
                      {alert("it's >= than 2")} 
                     break; 
    case "SINGLE": if (true_ans_array[i].length == 1) 
                      {alert("it's == to 1")}
                     break; 
    case "DESC": if (true_ans_array[i].length == 0) 
                      {alert("it's == to 0")}
                     break; 
    default: alert("neither Multiple, Single or Desc");

}

}

Leon
  • 867
  • 7
  • 13
0

You could iterate through both arrays at the same time, checking if they have the values you expect at each position.

Just rmember to check if both arrays have the same size and if the value at each position is a string. null.length will throw an error.

for ( var i = 0; i < qtype_array.length; i++ ) {
    var q = qtype_array[i];
    var a = true_ans_array[i];
    switch ( q ) {
        case "MULTIPLE":
            if ( a.length < 2 ) {
                alert('q == "MULTIPLE" but a.length < 2');
            }
            break;
        case "SINGLE":
            if ( a.length != 1 ) {
                alert('q == "SINGLE" but a.length != 1');
            }
            break;
        case "DESC":
            if ( a.length > 0 ) {
                alert('q == "DESC" but a.length > 0');
            }
            break;
        default:
            alert('q != "MULTIPLE", "SINGLE" and "DESC"');
            break;
    }
}
Community
  • 1
  • 1