# When should I use if/else if/else statements? When should I use switch statements?

Not sure if this is shorter, but maybe this solution will help somebody… Is it just me, or does this solution seem easier to read, and perhaps maintain down the line. Just the way it is formatted it is very self contained. And simple too, lol. Anyways, here it is, I used:

As a SWITCH

``````const calculateWeight = (earthWeight, planet) => {
const mercuryWeight = earthWeight * 0.378;
const venusWeight = earthWeight * 0.907;
const marsWeight = earthWeight * 0.377;
const jupiterWeight = earthWeight * 2.36;
const saturnWeight = earthWeight * 0.916;

switch (planet) {
case "Mercury":
return mercuryWeight;
case "Venus":
return venusWeight;
case "Mars":
return marsWeight;
case "Jupiter":
return jupiterWeight;
case "Saturn":
return saturnWeight;
default:
return "Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn."
}
}
``````

or as IF…ELSE

``````const calculateWeightTwo = (earthWeight, planet) => {
const mercuryWeight = earthWeight * 0.378;
const venusWeight = earthWeight * 0.907;
const marsWeight = earthWeight * 0.377;
const jupiterWeight = earthWeight * 2.36;
const saturnWeight = earthWeight * 0.916;
if (planet === "Mercury") {
return mercuryWeight;
} else if (planet === "Venus") {
return venusWeight;
} else if (planet === "Mars") {
return marsWeight;
} else if (planet === "Jupiter") {
return jupiterWeight;
} else if (planet === "Saturn") {
return saturnWeight;
} else {
return "Invalid Planet Entry. Try Mercury, Venus, Mars, Jupiter, or Saturn.";
}
}
``````

Also, Why exactly is codeCademy warning us NOT to post our answers? As a BRAND NEW developer, I LIKE SEEING other peoples solutions, it helps me LEARN as well. Before reading this thread I literally HAD NO IDEA there were so many different ways this could be written. LIke somehow I literally thought it could only be written in one or 2 very specific ways!! Now I just learned so much just by seeing HOW else it COULD have been done, regardless whose is shorter, it still TEACHES one the NATURE of code (or this specific language anyway!!)

(post deleted by author)

This was my solution for this one, any recommendations on what I could have done to make it better?

``````const calculateWeight = (earthWeight, planet) => {
if (planet === 'Mercury' || 'Venus' || 'Mars' || 'Jupiter' || 'Saturn') {
switch(planet) {
case 'Mercury':
return earthWeight * 0.378;
case 'Venus':
return earthWeight * 0.907;
case 'Mars':
return earthWeight * 0.377;
case 'Jupiter':
return earthWeight * 2.36;
case 'Saturn':
return earthWeight * 0.916;
default:
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
}
}
}
``````

I used the following code, and it works well:

const calculateWeight = (earthWeight, planet) => {

if (planet === ‘Mercury’) return earthWeight * 0.378;
if (planet === ‘Venus’) return earthWeight * 0.907;
if (planet === ‘Mars’) return earthWeight * 0.377;
if (planet === ‘Jupiter’) return earthWeight * 2.36;
if (planet === ‘Saturn’) return earthWeight * 0.916;
else return ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’
}

I know this is a question that may be asked often.
As you can see I used “if” for each conditioning (and not “else if”), with only one “else” only at the end, to define the exception.

The result is the same result in the end, but I would love if you could explain why still using “else if” has an advantage over using multiple “if”.

Maybe because of a load on the memory at a time when all the “if” conditions in my code above are checked at the same time, and with “else if” each one is checked in its turn until a condition is found to be true?

Are there cases where it is justified to use multiple "if as I did? if so, I would love an example.

A little important for me to understand.

a little late to the party but do use else if’s. From experience if you don’t your next if could overwrite the answer. May not be so obvious in this example.

Any ideas why i can’t pass calculateWeight()
It’s working so i have no idea.

``````

const calculateWeight = (earthWeight, planet) => {
if (planet === 'Mercury') {
return planet + ' weight equals ' + earthWeight * 0.378
} else if (planet === 'Venus') {
return planet + ' weight equals ' + earthWeight * 0.907
} else if (planet === 'Mars') {
return planet + ' weight equals ' + earthWeight * 0.377
} else if (planet === 'Jupiter') {
return planet + ' weight equals ' + earthWeight * 2.36
} else if (planet === 'Saturn') {
return planet + ' weight equals ' + earthWeight * 0.916
} else {
return
'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'}
};

console.log(calculateWeight(100, 'Jupiter')) // Should print 236

``````

Fortunately, the assertion or test setup in CodeCademy for this particular lesson does not test solutions other than the 5 given planets, or else your code would have failed the task. Let me breakdown to you as of why:

`````` if (planet === 'Mercury' || 'Venus' || 'Mars' || 'Jupiter' || 'Saturn')
``````

This conditional statement will always evaluate to `true`, despite planet not equal to `Mercury`. This is solely because JS treats any string (as long as it is not an empty string → `""`) as truthy value when performing logical operations.

``````  if (planet === 'Mercury' || planet == 'Venus' || planet == 'Mars' || planet == 'Jupiter' || planet == 'Saturn')
``````if(["Mercury", "Venus", "Mars", "Jupiter", "Saturn"].includes(planet))