Peter
Peter

Reputation: 49

switch/case always returns default value

I have the following problem: my function always returns the default value, no matter what I try. When I use if/else statements everything works fine. Where is the failure?

function auswertung(stueckzahl) {

	var preis1 = 0.77;
	var preis2 = 0.76;
	var preis3 = 0.73;
	var preis4 = 0.69;
	var preis5 = 0.67;

		switch(stueckzahl) {
			
		case ((stueckzahl>=500) && (stueckzahl <= 1000)): {
			return preis1;
		}
		case ((stueckzahl >= 1001) && (stueckzahl <= 2500)): {
			return preis2;
		}
		case ((stueckzahl >= 2501) && (stueckzahl <= 5000)): {
			return preis3;
		}
		case ((stueckzahl >= 5001) && (stueckzahl <= 10000)): {
			return preis4;
		}
		case ((stueckzahl >= 10001) && (stueckzahl <= 30000)): {
			return preis5;
		}
		default: {
			return preis1;
		}

		}
	}
		
document.write (auswertung(10000));

Upvotes: 4

Views: 4730

Answers (5)

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

You can perfectly use a switch with a boolean true as expression to re-evaluate your cases one by one.

This way your code stays clean:

function auswertung(stueckzahl) {
  switch (true) {
    case stueckzahl >= 10001 && stueckzahl <= 30000:
      return 0.67;
    case stueckzahl >= 5001 && stueckzahl <= 10000:
      return 0.69;
    case stueckzahl >= 2501 && stueckzahl <= 5000:
      return 0.73;
    case stueckzahl >= 1001 && stueckzahl <= 2500:
      return 0.76;
    case stueckzahl >= 500 && stueckzahl <= 1000:
    default:
      return 0.77;
  }
}

console.log('500:', auswertung(500));
console.log('10000:', auswertung(10000));
console.log('30000:', auswertung(30000));

Upvotes: 8

Rounin
Rounin

Reputation: 29463

You need to include break; after each case statement.

e.g.

switch (true) {
    case ((stueckzahl >= 500) && (stueckzahl <= 1000)): return preis1; break;
    case ((stueckzahl >= 1001) && (stueckzahl <= 2500)): return preis2; break;
    [...]
}

Otherwise the script will go on to check the next case.

Without break;, the script will always finish by running the default case.

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68393

If you are passing an expression to the case statement then it is evaluated first.

As per spec

  1. Let exprRef be the result of evaluating Expression.

  2. Let switchValue be GetValue(exprRef).

So,

(stueckzahl>=500) && (stueckzahl <= 1000)

will be either true or false.

Which means unless stueckzahl is true/false, it will always go to default section.

You need to replace this switch statements with if/else by doing. (also just check the upper limit in every condition)

    if ( stueckzahl <= 1000) {
        return preis1;
    }
    if ( stueckzahl <= 2500 ) {
        return preis2;
    }
    if ( stueckzahl <= 5000 ) {
        return preis3;
    }
    if ( stueckzahl <= 10000 ) {
        return preis4;
    }
    if ( stueckzahl <= 30000 ) {
        return preis5;
    }
    else {
        return preis1;
    }

Upvotes: 2

Juan
Juan

Reputation: 3705

The way switch works is the follwoing.

Switch expression is compared to the the different case values. And the code under a case is executed if that comparison evaluates true.

Your switch expression is stueckzahl

One of the case expressions values is: ((stueckzahl>=500) && (stueckzahl <= 1000)) which will evaluate to true or false depending on the value of stueckzahl.

So at the end what you will be comparing is: stueckzahl == true or stueckzahl == false which will never be true if you pass numbers as parameters for your function.

In your case looks that what you want is a chain of ifs:

    if ((stueckzahl>=500) && (stueckzahl <= 1000)) {
        return preis1;
    }
    if ((stueckzahl >= 1001) && (stueckzahl <= 2500)) {
        return preis2;
    }
    if ((stueckzahl >= 2501) && (stueckzahl <= 5000)) {
        return preis3;
    }
    if ((stueckzahl >= 5001) && (stueckzahl <= 10000)) {
        return preis4;
    }
    if ((stueckzahl >= 10001) && (stueckzahl <= 30000)) {
        return preis5;
    }

    return preis1;

More details in: http://www.w3schools.com/js/js_switch.asp

Upvotes: 0

NMC
NMC

Reputation: 1326

Your switch expects "stueckzahl" to be a value, not "true" or "false", and that is what "(stueckzahl>=500) && (stueckzahl <= 1000)" returns.

You have to use "if/else" statements in order to achieve what you want

Upvotes: 0

Related Questions