Do I need to use break or return?

Why doesn’t the solution in this exercise use:
break;

after each case statement?

3 Likes

If the case statements include a return, then break is unreachable and superfluous.

case 1:
  return a;
  break;    // not needed as it is unreachable
24 Likes

This gives the proper answer but also throws an undefined. When attempting to debug, it tells me that calculateWeight isn’t defined.

// Write your function here:
const calculateWeight = (earthWeight, planet) => {
	switch (planet) {
    case 'Murcury':
    	console.log(earthWeight * 0.378);
    	break;
    case 'Venus':
    	console.log(earthWeight * 0.907);
    	break;
    case 'Mars':
    	console.log(earthWeight * 0.377);
    	break;
    case 'Jupiter':
    	console.log(earthWeight * 2.36);
      break;
    case 'Saturn':
    	console.log(earthWeight * 0.916)
    	break;
    default:
    	console.log('Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.')
      break;
  }};

// Uncomment the line below when you're ready to try out your function
console.log(calculateWeight(100, 'Jupiter')) // Should print 236

// We encourage you to add more function calls of your own to test your code!
2 Likes

The function logs an outcome then the above line logs the return value of the function, which is undefined. Suggest change all the lines in the switch to return a value rather than log it.

case 'Mercury':
    return earthWeight * 0.378;

Also, remove the break keyword from all cases.

9 Likes

This makes sense, using return and then removing the breaks. Taking your advice, it’s now returning only the answer, but still not passing the test.

const calculateWeight = (earthWeight, planet) => {
	switch (planet) {
    case 'Murcury':
    	return earthWeight * .378;
    case 'Venus':
    	return earthWeight * .907;
    case 'Mars':
    	return earthWeight * .377;
    case 'Jupiter':
    	return earthWeight * 2.36;
    case 'Saturn':
    	return earthWeight * .916;
    default:
    	return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
  }};

Is there an error message given? Check your spelling of ‘Mercury’.

Apparently I just needed to reload. Same code, this time it passed. Thank you for the help!

1 Like

Can we generalize this as saying “if there is return in a switch statement, then break is not needed”?

1 Like

return does break the flow, but it also directs flow out of the function, immediately. We don’t need break and nor will the keyword be seen if it follows return. Nothing after return is reachable.

14 Likes

May I ask why would you omit the ‘break’ ?

Why are we taught break then, when first learning about switch? Is there a case example where break is necessary (Pretty much all examples I’ve used switch in involves returning on a case)?

1 Like

break after return is unreachable. To have it in the code is totally redundant.

Consider,

switch () {}, is a single conditional block. break is how we break out of that block.

return is how we jettison a function and go back to the caller with a payload.

A switch can be either in global or function scope, and return is sort of foreign to its fall-through built in effect that leads to the bottom of the block, the natural (default) exit, barring any breaks.

The key is to recognize this code pattern in both perspectives. A switch with return cannot be prototyped but one with break can be. Why, we ask? A prototype is portable. It’s behavior is self-contained. return means this construct is running inside a function. Remember that part about global or local?


Now consider,

  default: y = 0; break;
}

Does default need the break? Short pause… No. Why? It falls through to the end of the block, the natural break.

2 Likes

Hey!
Just trying to figure out how to do this another way!

let m = 0;
const calculateWeight = (earthWeight, planet) => {
    switch (planet) {
        case 'Mercury':
            m =.378;
        case 'Venus':
            m = .907;
        case 'Mars':
            m = .377;
        case 'Jupiter':
            m = 2.36;
        case 'Saturn':
            m = .916;
        default:
        return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
    }
    return earthWeight * m;
}
console.log(calculateWeight(100, "Jupiter"));

This doesn’t actually solve it because we always reach the default. One solution would be:

if (m != 0) {
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.' }
else { return earthWeight * m; }

But it doesn’t actually function properly. Would you recommend something else?

You will need to add a break at the end of each case.

2 Likes

That was easy :blush:

1 Like

bruh, “Nothing after return is reachable”

You just opened a thousand eyes in my mind :thinking:

3 Likes

Hello all!

So I tried this code but it doesn´t pass the test. I gives the proper values, but when we type a different planet it logs the error message + undefined.

why?

switch() should only take one argument, why do you use two?

the default case doesn’t return anything, so you get undefined.

1 Like

So switch should always take just one value right?

Thanks!

yes, this value is then compared against all cases.