...

View Full Version : switch()



scriptkeeper
06-01-2003, 05:51 AM
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!');
}

}

joh6nn
06-01-2003, 06:43 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum