-2

I have 3 html pages where 2 pages each have question 1 and question 2. The 3rd page shows the score of the test. I have designed the pages and now i want to keep track of the user scores. I'm not allowed to use local storage, cookies, database or sessions. I am suppose to pass the variable(to track each users answers) from one html page to another without using local storage or sessions, or cookies, and a DB.

Now in my external js file, have functions getAnswer1(form) which stores the user answer and passes the array to quiz_2.html file. I found an example online to pass a variable but having trouble retrieving the array in second function called getAnswer2(form) and storing the users second input. How do I go about that?

HTML Page 1: First page user selects 2 answers. A simple fill in the blank question and retrieve the answer. As you can see I assigned the right answers with correct.

<div>
                <form>
                    <input type="radio" name="one" value="correct" class="firstRow">&nbsp;Option 1 Mark 1: $650
                    <input type="radio" name="two" value="correct" class="secondRow">&nbsp;Option 1 Mark 2: Twitter<br>
                    <input type="radio" name="one" value="incorrect" class="firstRow">&nbsp;Option 2 Mark 1:$550
                    <input type="radio" name="two" value="incorrect" class="secondRow">&nbsp;Option 2 Mark 2:Google<br>
                    <input type="radio" name="one" value="incorrect" class="firstRow">&nbsp;Option 3 Mark 1:$650
                    <input type="radio" name="two" value="incorrect" class="secondRow">&nbsp;Option 3 Mark 2:$650<br>

                    <input type="button" value="Submit & Next Question" onclick="getAnswer1(this.form)" class="firstRow">
                    <input type="button" value="Cancel & Clear Selection" onclick="clearOptions(this.form)">
                </form>
            </div>

I have a external js file that holds the function to store the answers and performs necessary computation. I store the users answers in an array. I called the first function:

  function getAnswer1(form) {
     var results = [];
     var a = [];
     var value;

     var checked = form.querySelectorAll("input[type=radio]:checked");

   if(checked.length<2) {
       alert('Please select an option');
          return;
        }
 else {
         var n = checked.length;
            for(var i=0;i<n;i++) {
            a.push(checked[i].value);
           }
       }
       results.push(encodeURIComponent('key')+'='+encodeURIComponent(a))
       location.href = 'quiz_2.html?'+results.join('&');
  }

The second question:

<div>
                <form>
                    <input type="radio" name="radio" value="correct" class="firstRow"> NASA.Gov
                    <input type="radio" name="radio" value="incorrect" class="secondRow"> Data.Gov <br>
                    <input type="radio" name="radio" value="incorrect" class="firstRow"> Facebook
                    <input type="radio" name="radio" value="incorrect" class="secondRow"> XYZ.net <br>

                    <input type="button" value="Submit & Next Question" onclick="getAnswer2(this.form)" class="firstRow">
                    <input type="button" value="Cancel & Clear Selection" onclick="clearOptions(this.form)" class="secondRow">


                </form>
           </div>

Javascript:

As you can see below here I should be accepting a users 2nd test input and report the final scores to the survey but having difficulty retrieving the array variable and storing the answer.

function getAnswer2(form) {
    var value;
    var retrieveArray = {};
    var fs = location.search.replace('?', '').split('&');
    for(var i=0,l=fs.length; i<l; i++){
     var z = fs[i].split('=');
    retrieveArray[decodeURIComponent(z[0])] = decodeURIComponent(z[1]);
    }
    var checked =  form.querySelector("input[type=radio]:checked");
    if(!checked){
        alert('Please select an option');
       }
     else{
        value = checked.value;
      }
     location.href = "survey.html";
}
halfer
  • 19,824
  • 17
  • 99
  • 186
EI-01
  • 1,075
  • 3
  • 24
  • 42
  • First of all I would like to confirm my understanding. You want to pass the score without using any type of storage from one page to another page. Here I would suggest you to not use seperate pages and use the wizard instead. Where you can have all the data in a single page. – Dhaval Mar 05 '16 at 09:24
  • @Dhaval i'm not allowed to use local, session, cookies or DB. Required to pass the variable from one page to another. The only way ides i came up with was to store each answer in array and pass the array variable from one page to another so as to refer to that array at the end to calculate the scores – EI-01 Mar 05 '16 at 09:28
  • Yes, I understood that. However my point is from your base. Why to use seperate pages while you can easily integrate and manage with a wizard. That would be easy for your goal as well. – Dhaval Mar 05 '16 at 09:31
  • @Dhaval Each question on a page and links to the next question – EI-01 Mar 05 '16 at 09:46
  • @Dhaval each page has a question and links to the next question – EI-01 Mar 05 '16 at 09:56
  • Hi user3497437, you are either Jennifer or classmate of hers, cos another forum member and I spent a bunch of time answering / assisting with this exact question earlier today. You need o do some research before posting in here cos the only true way of learning is to try stuff and see what does / doesn't work. If you are not Jennifer then do some searching around this forum to find her posts cos you are doing the same question! – gavgrif Mar 05 '16 at 10:43
  • this is not a resource to answer clas questions if the student does not do any work and cos I am seeing php's answer (code names used in this post) in your code methinks you did not read my post earlier about learning for yourself! – gavgrif Mar 05 '16 at 10:44
  • "JF" (code name used) - just try doing it foryourself - you will feel so much more satisfied when you work it out yourself - do not just rely on others to tell you the answer. Learning is more than copying! – gavgrif Mar 05 '16 at 10:46

1 Answers1

-1

Sorry if my comments seemed harsh, but I am all about new coders learning the basics themselves without relying on having the answers provided. Just to show it can be done - I just created three HTML pages. Created a form in the first two - each with your questions (questions 1 and 2 in the first page and question 3 in the second page), and passed the form values to the next one, using nothing more than html.

Then using only JavaScript on the second and third pages, I grabbed the values out of the URL and did stuff with them. On page two, I re-used the values from page 1 (think how that might have been done and why it is useful) so that all three values are passed to page 3 which then used JavaScript only to grab the 3 values, display them on the page (as shown in the code section below) and calculate the total and the percentage of answers that are correct. Note that I answered the questions so that I got question 2 incorrect.

Note that I am not giving the code used, but will give you the URL of the pages so that you can see the outcome of the previous two pages and can then start to think how I achieved this.

numbers1.html
numbers2.html?one=correct&two=incorrect
numbers3.html?one=correct&two=incorrect&three=correct

Question 1: correct

Question 2:incorrect

Question 3:correct

2/3

0.67% correct

Not a traditional answer I know, but it is not ideal for learners simply ask for the answer to be provided, especially when in 10 minutes I was able to put together the three pages that achieved the outcome. If you do not try then you will not learn for yourself.

miken32
  • 42,008
  • 16
  • 111
  • 154
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • It looks like you meant to place this elsewhere - now that [you have done so](http://stackoverflow.com/a/35813699/472495) would you delete this one? – halfer Mar 05 '16 at 12:43
  • no - i answered the post on this page as an answer for this user - suspecting it was the same user as the other one, but it have been a different user (with the same question such as a class mate of the other poster). And then posted the same answer on the other page in case it was a different user. Same answer - potentially two posters... or just the one. BTW - sorry about the typoes - I didn't realise that there were people who edited the crappy typing - I will be more accurate with furture posts to prevent you having to clean up my crappy typing. Thanks – gavgrif Mar 05 '16 at 12:47
  • Ah, if the questions are identical (such that the same answer may be used for both) then perhaps one should be made a duplicate of the other. I'll take a look. Note that we generally discourage posting the same thing more than once, as any improvements to one would then need to be duplicated as well. – halfer Mar 05 '16 at 12:49
  • Yes, we have a lot of volunteer edit work going on here. – halfer Mar 05 '16 at 12:51