IF statement for a beginner

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • OrdinarySpore@gmail.com

    IF statement for a beginner

    Hi. I am having a problem with a simple IF statment on this
    Javascript. I found this script on scriptsource and want to see if I
    can add an IF statment to display a "congradulation s" line of text if
    the 5 questions on the Quiz are correct. I'm stumped and I think it's
    somthing simple but I thought it would work if I added the statement
    after the "Check Answer fuction like this ------- IF var score 5) {
    }


    Please help if you can. Thanks. I plan to change and minipulate it
    if it's worth it.

    <html>
    <head>
    <title>Trivia Quiz v 1.0 by POZ (poz@yahoo.com) </title>
    <style type="text/css">
    a,a:active, a:visited {color:blue; text-decoration:none ;}
    a:hover {color:red; text-decoration:none ;}
    </style>
    <script language="javas cript">
    /*
    Trivia Quiz v 1.0
    by POZ
    comments, bug reports, suggestions will be welcome at
    poz@yahoo.com
    may be used freely as long as this credit is maintained.
    */

    /*
    Easy customizing instructions:

    1. Add as many questions as you like according to the structure of the
    existing demo questions.
    Make sure that every question is properly enveloped with <div
    id="questionX" style="display: none">...</div>
    where X is the number of the question (numbers must be sequential)
    2. Adjust the correctAnswers array to hold the letter of the correct
    answer for each question.
    3. Enjoy.
    */

    // how many seconds available to answer each question
    var delay=10;

    // an array listing the correct answer for each question in order of
    appearance
    var correctAnswers= new Array("c","d"," b","d","c");

    // =============== =============== =============== ========
    // do not change script below this line
    // =============== =============== =============== ========
    var q_num=1;
    var timer;
    var layer;
    var clock=delay;
    var score=0;

    function show_question() {
    if (layer=eval("do cument.all.ques tion"+q_num)){
    layer.style.dis play="inline";
    document.all.ti meLeft.innerHTM L=clock;
    hide_question() ;
    } else {
    document.all.ti meLeft.innerHTM L=0;
    document.all.qu izScore.innerHT ML="You have answered correctly "+score
    +" out of "+(q_num-1)+" questions.";
    document.all.qu izScore.style.d isplay="inline" ;
    }
    }

    function hide_question() {
    if (clock>0) {
    document.all.ti meLeft.innerHTM L=clock;
    clock--;
    timer=setTimeou t("hide_questio n()",1000);
    } else {
    clearTimeout(ti mer);
    document.all.an swerBoard.inner HTML=" ";
    clock=delay;
    layer.style.dis play="none";
    q_num++;
    show_question() ;
    }
    }

    function check_answer(an swer){
    if (correctAnswers[q_num-1]==answer){
    score++;
    document.all.an swerBoard.inner HTML="<i><b>Cor rect!</b></i>";
    } else {
    document.all.an swerBoard.inner HTML="<i><b>Wro ng!</b></i>";
    }
    clock=0;
    clearTimeout(ti mer);
    timer=setTimeou t("hide_questio n()",1500);
    }

    window.onload=s how_question;
    </script>
    </head>

    <body>

    Time left: <span id="timeLeft"> </span seconds<br>
    <br>
    <div id="answerBoard "</div>
    <br>

    <div id="question1" style="display: none">
    <b>1. Who was the first man on the moon?</b><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('a')">a) Captain
    Kirk</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('b')">b) Tom Hanks</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('c')">c) Neil
    Armstrong</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('d')">d) Bugs
    Bunny</a><br>
    </div>

    <div id="question2" style="display: none">
    <b>2. What is the largest land mammal on earth?</b><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('a')">a) John
    Goodman</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('b')">b) Yo Mama</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('c')">c) Humpback
    Whale</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('d')">d) African
    Elephant</a><br>
    </div>

    <div id="question3" style="display: none">
    <b>3. Why?</b><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('a')">a) Because</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('b')">b) Why not?</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('c')">c) Who?</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('d')">d) He did it!
    </a><br>
    </div>

    <div id="question4" style="display: none">
    <b>4. Which of the following is the longest?</b><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('a')">a) one foot</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('b')">b) one
    kilometer</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('c')">c) one mile</
    a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('d')">d) 753 light
    years</a><br>
    </div>

    <div id="question5" style="display: none">
    <b>5. Which of the following actors is least likely to ever win an
    oscar?</b><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('a')">a) Bruce
    Willis</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('b')">b) James
    Gandolfini</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('c')">c) Steven
    Seagal</a><br>
    <a href="javascrip t:void(0)" onclick="check_ answer('d')">d) Mr. T</
    a><br>
    </div>

    <div id="quizScore" style="display: none">
    </div>

    </body>
    </html>

  • -Lost

    #2
    Re: IF statement for a beginner

    <OrdinarySpore@ gmail.comwrote in message
    news:1170132654 .917102.139150@ m58g2000cwm.goo glegroups.com.. .
    Hi. I am having a problem with a simple IF statment on this
    Javascript. I found this script on scriptsource and want to see if I
    can add an IF statment to display a "congradulation s" line of text if
    the 5 questions on the Quiz are correct. I'm stumped and I think it's
    somthing simple but I thought it would work if I added the statement
    after the "Check Answer fuction like this ------- IF var score 5) {
    }
    <snip script contents>

    Sure, you could use:

    if (score === 5) { alert('Yay, perfect score!'); }

    .... in the bottom of the check answer function.

    You could have found that from any search on Google though!

    I would also change it to adhere to standard DOM methods and properties. For example,
    docment.all is a no-no. You should always feature detect rather than browser detect which
    insures that you use DOM methods.

    -Lost


    Comment

    • Randy Webb

      #3
      Re: IF statement for a beginner

      -Lost said the following on 1/30/2007 12:08 AM:
      <OrdinarySpore@ gmail.comwrote in message
      news:1170132654 .917102.139150@ m58g2000cwm.goo glegroups.com.. .
      >Hi. I am having a problem with a simple IF statment on this
      >Javascript. I found this script on scriptsource and want to see if I
      >can add an IF statment to display a "congradulation s" line of text if
      >the 5 questions on the Quiz are correct. I'm stumped and I think it's
      >somthing simple but I thought it would work if I added the statement
      >after the "Check Answer fuction like this ------- IF var score 5) {
      >}
      >
      <snip script contents>
      >
      Sure, you could use:
      >
      if (score === 5) { alert('Yay, perfect score!'); }
      Be careful with === though unless you understand what is going to happen
      if you feed it "5" and want a true result. And, in the OP, IF != if

      --
      Randy
      Chance Favors The Prepared Mind
      comp.lang.javas cript FAQ - http://jibbering.com/faq/index.html
      Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/

      Comment

      Working...