Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Few Questions about a javascript based quiz game.

    Inside of here if you save as a .html and open it it will pull up the game a series of 5 boxes and 5 answers underneath, Currently those 5 answers correctly correlate with each box so you know what they are considering you don't have the .jpgs to display them. The one in bold is the "question" when you click on the one that correlates with the bold it should tell you Correct! 100. 100 being your score and if you get it wrong it will say sorry! and then take your score down to a 50 since you got 1 out of 2 wrong. The problem is when you get it wrong it also immediatly says Congrats! after you get it wrong So it will prompt a Sorry! alert and a Congrats! alert on the wrong answer.

    The second problem is after the user gets it right or wrong, I need it to re random the list of answers and images. Whilst keeping the score intact for up to 20 questions.

    Please help!



    Code:
    <body>
    <script type="text/javascript">
    var counter = 0;
    var correct = 0;
    function show_alert0()
    {
        {counter++;}
    if (randomname0 != final)  
       {alert("Sorry." + (( correct / counter ) * 100 ));}
     else
        {correct++;}
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
      
    }
    function show_alert1()
    {
        {counter++;}
    if (randomname1 != final)
       
       {alert("Sorry." + (( correct / counter ) * 100 ));}
     else
         {correct++;}
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
       
    }
    function show_alert2()
    {
        {counter++;}
    if (randomname2 != final)
       {alert("Sorry." + (( correct / counter ) * 100 ));}
     else
         {correct++;}
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
       
    }
    function show_alert3()
    {
        {counter++;}
    if (randomname3 != final)
       {alert("Sorry." + (( correct / counter ) * 100 ));}
     else
         {correct++;}
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
       
    }
    function show_alert4()
    {
        {counter++;}
    if (randomname4 != final)
       {alert("Sorry." + (( correct / counter ) * 100 ));}
     else
         {correct++;}
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
     
    }
    </script>
    <script type="text/javascript">
    var randomnumber0=Math.floor(Math.random()*66)
    var randomnumber1=Math.floor(Math.random()*66)
    var randomnumber2=Math.floor(Math.random()*66)
    var randomnumber3=Math.floor(Math.random()*66)
    var randomnumber4=Math.floor(Math.random()*66)
    while (randomnumber0==randomnumber1 || randomnumber0==randomnumber2 || randomnumber0==randomnumber3 || randomnumber0==randomnumber4)
      {randomnumber0=Math.floor(Math.random()*66)}
    while (randomnumber1==randomnumber0 || randomnumber1==randomnumber2 || randomnumber1==randomnumber3 || randomnumber1==randomnumber4)
      {randomnumber1=Math.floor(Math.random()*66)}
    while (randomnumber2==randomnumber0 || randomnumber2==randomnumber1 || randomnumber2==randomnumber3 || randomnumber2==randomnumber4)
      {randomnumber2=Math.floor(Math.random()*66)}
    while (randomnumber3==randomnumber0 || randomnumber3==randomnumber1 || randomnumber3==randomnumber2 || randomnumber3==randomnumber4)
     {randomnumber3=Math.floor(Math.random()*66)}
    while (randomnumber4==randomnumber0 || randomnumber4==randomnumber1 || randomnumber4==randomnumber2 || randomnumber4==randomnumber3)
     {randomnumber4=Math.floor(Math.random()*66)}
    document.write('<p style="text-align:center">')
    document.write('<em>Click on the ribbon that matches the name below.</em><br/><br/>')
    document.write('<button type="button" height="300" width="300" onclick="show_alert0()"><img src="https://www.intelink.gov/sites/afpaa/Shared%20Documents/rank'+randomnumber0+'.jpg" height="300px" width="200px"></button>')
    document.write('<button type="button" height="300" width="300" onclick="show_alert1()"><img src="https://www.intelink.gov/sites/afpaa/Shared%20Documents/rank'+randomnumber1+'.jpg" height="300px" width="200px"></button>')
    document.write('<button type="button" height="300" width="300" onclick="show_alert2()"><img src="https://www.intelink.gov/sites/afpaa/Shared%20Documents/rank'+randomnumber2+'.jpg" height="300px" width="200px"></button>')
    document.write('<button type="button" height="300" width="300" onclick="show_alert3()"><img src="https://www.intelink.gov/sites/afpaa/Shared%20Documents/rank'+randomnumber3+'.jpg" height="300px" width="200px"></button>')
    document.write('<button type="button" height="300" width="300" onclick="show_alert4()"><img src="https://www.intelink.gov/sites/afpaa/Shared%20Documents/rank'+randomnumber4+'.jpg" height="300px" width="200px"></button><br/>')
    document.write('</style>')
    var name = new Array();
    name[0] = "Airman";
    name[1] = "Airman 1st Class";
    name[2] = "Senior Airman";
    name[3] = "Staff Sergeant";
    name[4] = "Technical Sergeant ";
    name[5] = "Master Sergeant";
    name[6] = "First Master Sergeant";
    name[7] = "Senior Master Sergeant";
    name[8] = "First Senior Master Sergeant";
    name[9] = "Chief Master Sergeant";
    name[10] = "First Chief Master Sergeant";
    name[11] = "Command Chief Master Sergeant";
    name[12] = "Chief Master Sergeant of the Air Force";
    name[13] = "2nd Lieutenant";
    name[14] = "1st Lieutenant";
    name[15] = "Captain";
    name[16] = "Major";
    name[17] = "Lieutenant Colonel";
    name[18] = "Colonel";
    name[19] = "Brigadier General";
    name[20] = "Major General";
    name[21] = "Lieutenant General";
    name[22] = "General";
    name[23] = "General of the ...";
    name[24] = "Private";
    name[25] = "Private 1st Class";
    name[26] = "Specialist";
    name[27] = "Corporal";
    name[28] = "Sergeant";
    name[29] = "Staff Sergeant";
    name[30] = "Sergeant First Class";
    name[31] = "Master Sergeant";
    name[32] = "First Sergeant";
    name[33] = "Sergeant Major";
    name[34] = "Command Sergeant Major";
    name[35] = "Sergeant Major of the Army";
    name[36] = "Warrant Officer (W01)";
    name[37] = "Chief Warrant Officer (CW2) ";
    name[38] = "Chief Warrant Officer (CW3)";
    name[39] = "Chief Warrant Officer (CW4)";
    name[40] = "Chief Warrant Officer (CW5)";
    name[41] = "Private First Class";
    name[42] = "Lance Corporal";
    name[43] = "Corporal";
    name[44] = "Sergeant";
    name[45] = "Staff Sergeant";
    name[46] = "First Sergeant";
    name[47] = "Sergeant Major";
    name[48] = "Master Gunnery Sergeant";
    name[49] = "Warrant Officer (W0)";
    name[50] = "Chief Warrant Officer (CW02)";
    name[51] = "Chief Warrant Officer (CW03)";
    name[52] = "Chief Warrant Officer (CW04)";
    name[53] = "Chief Warrant Officer (CW05)";
    name[54] = "Seaman Apprentice";
    name[55] = "Seaman";
    name[56] = "Petty Officer Third Class";
    name[57] = "Petty Officer Second Class";
    name[58] = "Petty Officer First Class";
    name[59] = "Chief Petty Officer";
    name[60] = "Second Chief Petty Officer";
    name[61] = "Master Chief Petty Officer";
    name[62] = "Fleet / Command Master Chief Petty Officer";
    name[63] = "Master Chief Petty Officer of the Navy";
    name[64] = "Chief Warrant Officer (CW02)";
    name[65] = "Chief Warrant Officer (CW03)";
    name[66] = "Chief Warrant Officer (CW04)";
    var randomname0 = name[randomnumber0];
    var randomname1 = name[randomnumber1];
    var randomname2 = name[randomnumber2];
    var randomname3 = name[randomnumber3];
    var randomname4 = name[randomnumber4];
    document.write(randomname0+'<br/>'+randomname1+'<br/>'+randomname2+'<br/>'+randomname3+'<br/>'+randomname4+'<br/>')
    var randomname = new Array(5);
    randomname[0] = name[randomnumber0];
    randomname[1] = name[randomnumber1];
    randomname[2] = name[randomnumber2];
    randomname[3] = name[randomnumber3];
    randomname[4] = name[randomnumber4];
    var randomnamenum=Math.floor(Math.random()*4);
    var final=randomname[randomnamenum];
    document.write('<br/><br/><pstyle="text-align:center"><strong>'+final+'</strong></style><br>')
    </script>
    </body>
    Last edited by Doostinage; 02-16-2012 at 03:20 PM.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    First of all, please change your post to have [ code ] and [ /code ] around your code example. Otherwise it is really unreadable

    Yet ... the very first function looks like this
    Code:
    function show_alert0() {
       {counter++;}
       if (randomname0 != final) {
          alert("Sorry." + (( correct / counter ) * 100 ));
       } else {
          correct++;
       }
       {alert("Congrats!" + (( correct / counter ) * 100 ));}
    }
    which tells me that you have absolutely no idea about the usage of curly brackets. With curly brackets you enclose a logial group of statements (not just one) that will then be executed sequentially (in case of certain conditions).

    In your case the bottom most alert is inside of curly brackets OUTSIDE(!) of the else part of the if statement. This will make the alert appear no matter what the result of the if condition was.

  • Users who have thanked devnull69 for this post:

    Doostinage (02-16-2012)

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you for the 2 lessons. I am aware that I need to work on my Javascript however my boss is the one who did some of this. A little more code-less than me and I just have to agree on what she says.

    However, Thank you very much for your input it was very useful.

    Do you by any chance know how I can make it to where it re-randoms the images and names?

    Thank you again.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •