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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Javascript test urgent help!

    Dear All,

    I have been given the task of creating an IQ type test using Javascript - the quiz must generate a score at the END of the quiz eg. 75% well done!

    The IQ test must be implemented in raw Javascript - so must be written out, not generated by Dreamweaver etc. - my teacher will know and I will get 0!

    I have four questions only, so each is worth 25%.

    I have one question with radio-buttons, one with a pull-down menu, one with tick boxes, and one with a text field.

    HOWEVER I have never done this before and am completely stuck generating a score - I literally just have the questions laid out (please see below what ive done so far - don't laugh)

    ANY HELP MUCH NEEDED PLEASE!?

    <form>
    <ol>
    <form name="form1" method="post" action="">
    <p>What is the total amount of legs of two cats and 3 spiders?</p>
    <p><label><input type="radio" name="legs" value="1" onclick="answer(this);">36</label></p>
    <p><label><input type="radio" name="legs" value="2" onclick="answer(this);">32</label></p>
    <p><label><input type="radio" name="legs" value="3" onclick="answer(this);">28</label></p>
    <p><label for score="score">Score:</label><input name="score" type="text" id="score"></p>
    </form>

    <script>
    var globalScore = 0;
    function answer(selectedButton) {
    var score = document.getElementById("score");
    if (selectedButton.value == "2") {
    alert("You got the correct answer!");
    globalScore += 25;
    } else {
    alert("I am sorry, the correct answer is 32.");
    }

    score.value = globalScore;
    }
    </script>
    <br>
    </br>

    <form>
    <li>How many slices of pie result in making three cuts?</li>
    <ul>
    <select name="Pieces">
    <option value="1">Select Answer</option>
    <option value="2">6</option>
    <option value="3">8</option>
    <option value="4">4</option>
    <option value="5">10</option>
    </select>
    </form>
    </ul>
    <br>
    </br>

    <form>
    <li>Which two numbers add up to make 77? (tick all that apply)</li>
    <ul>
    <li><input type="checkbox" name="Q1" onclick="answer('a')">33 and 43</input></li>
    <li><input type="checkbox" name="Q1" onclick="answer('b')">69 and 9</input></li>
    <li><input type="checkbox" name="Q1" onclick="answer('c')">19 and 58</input></li>
    <li><input type="checkbox" name="Q1" onclick="answer('d')">24 and 53</input></li>
    </ul>
    <br>
    </br>

    <li>Which number should come next in this series? 1,3,5,7,9...</li>
    <br>
    </br>
    <textarea rows="5" cols="15">
    Answer here...
    </textarea>
    </ol>
    </form>
    </body>
    </div>
    </html>

  • #2
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    P.S. It must look like a complete amateur could have written it out lol

    Thanks guys

  • #3
    New to the CF scene
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It would help alot if you formatted this a bit nicer. Check out jsfiddle.com or codepen.io or http://liveweave.com/ for easy tidying up of code. I'm pretty new at this too but ill look through and see if I can help!

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,071
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Exclamation

    Quote Originally Posted by lauren66 View Post
    P.S. It must look like a complete amateur could have written it out lol

    Thanks guys
    You have a number of syntax errors in your HTML. You should check the error console frequently!!!

    Here is one partial solution.
    Make sure you understand how it works as I have added NO COMMENTS.
    If you need to defend your solution and can not explain it, you will fail.
    Note, I have not solved the last question so the best score you will get is a 75 if you turn it in as is.

    Code:
    <!doctype html>
    <html>
    <head>
    <title> </title>
    <script type="text/javascript">
    function getBtnBox(grp) {
      var pick = 0;
      var sel = document.getElementsByName(grp);
      for (var i=0; i<sel.length; i++) {
        if (sel[i].checked) { pick += Number(sel[i].value); }
      }
      return pick;
    }
    </script>
    
    </head>
    
    <form name="form1" method="post" action="">
    <p>What is the total amount of legs of two cats and 3 spiders?</p>
    <p><label><input type="radio" name="legs" value="0" onclick="answer();">36</label></p>
    <p><label><input type="radio" name="legs" value="25" onclick="answer();">32</label></p>
    <p><label><input type="radio" name="legs" value="0" onclick="answer();">28</label></p>
    
    <p>How many slices of pie result in making three cuts?</p>
    <ul>
     <select id="slices" name="Pieces" onchange="answer()"> 
      <option value="0">Select Answer</option>
      <option value="25">6</option>
      <option value="0">8</option>
      <option value="0">4</option>
      <option value="0">10</option>
     </select>
    </ul>
    
    <p>Which two numbers add up to make 77? (tick all that apply)</p>
    <ul>
     <li><input type="checkbox" name="Q1" value="0" onclick="answer()">33 and 43</input></li>
     <li><input type="checkbox" name="Q1" value="0" onclick="answer()">69 and 9</input></li>
     <li><input type="checkbox" name="Q1" value="25" onclick="answer()">19 and 58</input></li>
     <li><input type="checkbox" name="Q1" value="0" onclick="answer()">24 and 53</input></li>
    </ul>
    
    <p>Which number should come next in this series? 1,3,5,7,9...</p>
    <br> </br>
    <textarea rows="5" cols="35">... Put answer here...</textarea>
    
    <p><label for score="score">Score:</label><input name="score" type="text" id="score"></p>
    
    </form>
    
    <script>
    var globalScore = 0;
    function answer() {
      var score = document.getElementById("score");
      var sum = Number(getBtnBox('legs'));
      sum += Number(document.getElementById('slices').value);
      sum += Number(getBtnBox('Q1'));
      score.value = sum;
    }
    </script>
    
    </body>
    </html>
    BTW: You should start your homework assignments earlier in the term!!!


  •  

    Tags for this Thread

    Posting Permissions

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