Javascript Switch & Case

This is confusing me as to why a variable is declared undefined but when I hard code it it is fine.

var lamination = ;
var price;
lamination[“500”] = 60;lamination[“1000”] = 80;lamination[“2000”] = 100;lamination[“3000”] = 150;lamination[“4000”] = 200;

case ((qty)<=500): price=lamination[500];break;
case qty>500 && qty<=1000:price=lamination[1000];break;
case qty>1000 && qty<=2000:price=lamination[2000];break;
case qty>2000 && qty<=3000:price=lamination[3000];break;
case qty>3000 && qty<=4000:price=lamination[4000];break;

I’m not sure about what’s confusing you there. Can you please expand on what you find to be confusing?

What should help, is to realize that the cases have to be values - they can’t be comparisons.

Cases can be comparisons as it works fine in a php environment. And also putting hard code in to each case environment will produce the correct result but passing as a variable it still declares as undeifned.

Basically the case element works but assigning it to a variable declares undefined as a result.

Sorry, you’re wrong there. JavaScript is different from PHP and has different rules.

A good way to learn the details of what JavaScript is capable of, is via resources such as

So what are the basics of a javascript case comparison?

Are you saying I should do the comparison first and then assign it to the switch statement?

JavaScript uses values, such as primitive numbers and strings for the switch cases.

You can use a series of if statements instead, but I’ll see if I can come up with a smarter technique for you than that.

The following code works well and allows you to easily add in different price points as and where need be.

function getBestRate(qty, priceLimits) {
    var defaultLowestRate = priceLimits[0];
    return priceLimits.reduce(function compareRates(curRate, newRate) {
        if (qty > newRate.qty) {
            return curRate;
        return newRate;
    }, defaultLowestRate);
function laminationPrice(qty) {
    priceLimits = [
        {qty: 4000, price: 200},
        {qty: 3000, price: 150},
        {qty: 2000, price: 100},
        {qty: 1000, price: 80},
        {qty: 500, price: 60}
    var bestRate = getBestRate(qty, priceLimits);

    return bestRate.price;
for (qty = 490; qty < 4010; qty += 10) {
    console.log(qty, laminationPrice(qty));
1 Like

Not sure how your calculating that.
calling laminationPrice(100) returns 200 and not 60

change qty = 490 to qty = 0

Still giving 200 as a result and also doesn’t allow for greater than 4000 which is wrong too.
The qty given can range from 100 to 25,000 or more

Also don’t really want it to run through a loop as there is more going on than just this caclulation and multiple loops in the code will slow the overall calculator down.

Let’s keep it nice and simple then, working from your original code but using if statements instead of switch.

var lamination = [];
var price;
lamination["500"] = 60;lamination["1000"] = 80;lamination["2000"] = 100;lamination["3000"] = 150;lamination["4000"] = 200;

if (qty <= 500) {
    price = lamination[500];
if (qty > 500 && qty <= 1000) {
    price = lamination[1000];
if (qty > 1000 && qty <= 2000) {
    price = lamination[2000];
if (qty > 2000 && qty <= 3000) {
    price = lamination[3000];
if (qty > 3000 && qty <= 4000) {
    price = lamination[4000];

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.