if语句判断太繁琐,想改成switch,哪位能帮帮忙请问高手们,现在有三套电脑配置,有两种配置的价格一样,我这个只能判断两种,我想用switch来判断三种(或是多种),最后的价格,应该怎么做,请指教
有三种配置,应该会出现三种不同的价格,<html>
<head>
<script language="javascript">
function get_price()
{
var the_price=1000;
if(this.speed=="500MHZ")
{
the_price+=200;
}
else
{
the_price+=100;
} if(this.hdspace=="15GB")
{
the_price+=200;
}
else
{
the_price+=100;
}
if(this.ram=="128MB")
{
the_price+=150;
}
else
{
the_price+=75;
} return the_price;
}function computer(speed,hdspace,ram)
{
this.speed=speed;
this.hdspace=hdspace;
this.ram=ram;
this.price=get_price;
}
var work_computer = new computer("500MHZ","15GB","128MB");
var home_computer = new computer("450MHZ","10GB","64MB");
var laptop_computer = new computer("350MHZ","7GB","32MB");
var work_computer_price = work_computer.price();
var home_computer_price = home_computer.price();
var laptop_computer_price = laptop_computer.price();
</script>
</head>
<body>
<script language="javascript">
document.write("这里是第一种配置work_computer<br>");
document.write(work_computer.speed+","+work_computer.hdspace+","+work_computer.ram+"<br>");
document.write("共$ "+work_computer_price+"<br>");
document.write("这里是第二种配置home_computer<br>");
document.write(home_computer.speed+","+home_computer.hdspace+","+home_computer.ram+"<br>");
document.write("共$ "+home_computer_price+"<br>");
document.write("这里是第三种配置laptop_computer<br>");
document.write(laptop_computer.speed+","+laptop_computer.hdspace+","+laptop_computer.ram+"<br>");
document.write("共$ "+laptop_computer_price );
</script></script>
</body>
</html>
如果改成下面这样的switch语句,像下面这样写是不行的,有没有哪位有更好的办法,
switch (this.speed)
{
case "500MHZ":
the_price+=200;
break;
case "45MHZ":
the_price = ...;
break;
case "350MHZ":
the_price = ....
break;
case default:
break;
}
这样写是不行的,如果行的话怎么能用----挑战----这两个字呢?上面那样的我都试过,不行,