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 2 of 2

Thread: switch()

  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Northern California
    Posts
    169
    Thanks
    0
    Thanked 0 Times in 0 Posts

    switch()

    I was just wondering how to go about using a switch and/or case structure for conditional statments such as things like this

    //document.getElementById("the_table").rows[0].cells[0].innerHTML =
    "topleft"
    function displayPlan(){
    var row = document.getElementById("the_table").rows(1)

    if (plan.value=="nat1") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".60"
    row.cells[2].innerHTML = ".50"
    row.cells[3].innerHTML = ".40"
    row.cells[4].innerHTML = ".35"
    row.cells[5].innerHTML = ".20"
    }


    else if (plan.value=="ohmi1") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".55"
    row.cells[2].innerHTML = ".45"
    row.cells[3].innerHTML = ".35"
    row.cells[4].innerHTML = ".30"
    row.cells[5].innerHTML = ".20"
    }


    else if (plan.value=="usa1") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".33"
    row.cells[2].innerHTML = ".33"
    row.cells[3].innerHTML = ".26"
    row.cells[4].innerHTML = ".23"
    row.cells[5].innerHTML = ".13"
    }


    else if (plan.value=="usa2") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".35"
    row.cells[2].innerHTML = ".35"
    row.cells[3].innerHTML = ".30"
    row.cells[4].innerHTML = ".28"
    row.cells[5].innerHTML = ".25"
    }



    else if (plan.value=="usa3") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".30"
    row.cells[2].innerHTML = ".30"
    row.cells[3].innerHTML = ".30"
    row.cells[4].innerHTML = ".30"
    row.cells[5].innerHTML = ".30"
    }


    else if (plan.value=="flat1") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".20"
    row.cells[2].innerHTML = ".20"
    row.cells[3].innerHTML = ".20"
    row.cells[4].innerHTML = ".20"
    row.cells[5].innerHTML = ".20"
    }


    else if (plan.value=="zapso") {

    row.cells[0].innerHTML = "<b>Rate:"
    row.cells[1].innerHTML = ".35"
    row.cells[2].innerHTML = ".15"
    row.cells[3].innerHTML = ".10"
    row.cells[4].innerHTML = ".07"
    row.cells[5].innerHTML = ".06"
    }
    else {
    alert('Price Plan Not found!');
    }

    }
    -When you do things right, people won't be sure you've done anything at all

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    so, um, i started out, working on doing this with the switch statement. then, i said "oh, i could do this ... ". then i noticed "oh, i could tweak this..."

    in the end, i came out with this:

    function displayPlan() {
    var row = document.getElementById("the_table").rows(1)
    var i=1, tempArray;
    var vals1to5 = {
    nat1: [,".60",".50",".40",".35",".20"],
    ohmi1: [,".55",".45",".35",".30",".20"],
    usa1: [,".33",".33",".26",".23",".13"],
    usa2: [,".35",".35",".30",".28",".25"],
    usa3: [,".30",".30",".30",".30",".30"],
    flat1: [,".20",".20",".20",".20",".20"],
    zapso: [,".25",".15",".10",".07",".06"]
    };
    tempArray = vals1to5[plan.value];

    if (/string|number|boolean/.test(typeof tempArray)) {
    row.cells[0].innerHTML = "<b>Rate:"
    for ( i; i < 6; i++ ) {
    row.cells[i].innerHTML = tempArray[i];
    }
    }
    else {
    alert('Price Plan Not found!');
    }

    }


    i didn't test it, but it should work
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.


  •  

    Posting Permissions

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