Why is this the case that this error pops up

// Write your function here:
const calculateWeight = (earthWeight,planet) =>{
  if (planet === 'Mercury')
    return 'The weight on planet Mercury is ' + earthWeight * 0.378;
  else if (planet === 'Venus')
    return 'The weight on planet Venus is ' + earthWeight * 0.907;
  else if (planet === 'Mars')
    return 'The weight on planet Mars is ' + earthWeight * 0.377;
  else if (planet === 'Jupiter')
    return 'The weight on planet Jupiter is ' + earthWeight * 2.36;
  else if (planet === 'Saturn')
    return 'The weight on planet Saturn is ' + earthWeight * 0.916;
  else
    return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
  /* ----Commednted this section out:[calculateWeight](https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/calculate-weight)
  switch (earthWeight,planet){
    case "Mercury":
    	return 'The weight on planet Mercury is ' + earthWeight * 0.378;
    case "Venus":
      return 'The weight on planet Venus is ' + earthWeight * 0.907;
    case "Mars":
      return 'The weight on planet Mars is ' + earthWeight * 0.377;
    case "Jupiter":
      return 'The weight on planet Jupiter is ' + earthWeight * 2.36;
    case "Saturn":
      return 'The weight on planet Saturn is ' + earthWeight * 0.916;
    default:
    return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
    }  */
};

// 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!
 console.log(calculateWeight(100, 'Mercury')) // Should print 387

When I ran the code above I got the outputs I wanted but the following error code appeared: When the planet is Mercury, the function should return the weight passed in multiplied by .378

My question is why is this the case that this error pops up as I have got through the code and maybe there is something missing? Because I cannot see it.

Can anyone kindly break this down.

Regards

the exercise validation expects a number as result. Doing more then required in an exercise can cause problems

We’d have to assume that this is inside the function…

  switch (earthWeight,planet){
    case "Mercury":
    	return 'The weight on planet Mercury is ' + earthWeight * 0.378;
    case "Venus":
      return 'The weight on planet Venus is ' + earthWeight * 0.907;
    case "Mars":
      return 'The weight on planet Mars is ' + earthWeight * 0.377;
    case "Jupiter":
      return 'The weight on planet Jupiter is ' + earthWeight * 2.36;
    case "Saturn":
      return 'The weight on planet Saturn is ' + earthWeight * 0.916;
    default:
    return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
    } 
};

As already mentioned, if the switch was not asked for, then it would raise a flag.

Aside

calculateWeight

Now, consider the switch parameters as written above…

switch (earthWeight,planet){

earthWeight is a constant so does not need to be in the parameter, and furthermore there should be an expression, not a parameter list. A switch is not like a function that can take multiple parameters.

switch (planet) {

}

Edit

Just looked at the exercise and it looks quite open-ended in terms of which approach we use. That means you could use a switch but it must contain only one parameter (an expression).

The switch does fit this problem quite ideally.

Up to this point, what of the below have you not yet covered?

  • objects
  • hasOwnProperty
  • ternary expression statements
  • unary operators

I ask because there is another approach to this problem that uses neither if nor swtich. Let us know if you wish to explore it, provided you’re familiar with the above concepts.

2 Likes

Thanks mtf for the response. For clarity based on your explanation, does it mean that the switch statement always takes a single parameter all the time?

Also if that is the case, I also made use of the if statement to solve the problem and it is the same issue you can have a look at that as well. It does what the program asks yet it still prints the same error “When the planet is Mercury, the function should return the weight passed in multiplied by .378

The code is shown below:

// Write your function here:
const calculateWeight = (earthWeight,planet) =>{
  if (planet === 'Mercury')
    return 'The weight on planet Mercury is ' + earthWeight * 0.378;
  else if (planet === 'Venus')
    return 'The weight on planet Venus is ' + earthWeight * 0.907;
  else if (planet === 'Mars')
    return 'The weight on planet Mars is ' + earthWeight * 0.377;
  else if (planet === 'Jupiter')
    return 'The weight on planet Jupiter is ' + earthWeight * 2.36;
  else if (planet === 'Saturn')
    return 'The weight on planet Saturn is ' + earthWeight * 0.916;
  else
    return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
};
 console.log(calculateWeight(100, 'Mercury')) // Should print 37.8

Finally, I am yet to cover objects, hasOwnProperty, ternary expression statements and unary operators.

Look forward to your response

That’s right. The parameter can take the form of a value literal, a varaiable (a value reference) or an expression, either literal or in a variable. The key term here is expression.

switch (expr1) {
case expr2:

}

When the two expressions resolve to the same value, the case is a match and that branch is followed.

The cause of the error may be the additional text included in the return value which the SCT is not expecting. Note the instruction…

return a number representing what that Earth-weight would equate to on the planet passed in.

That suggests only the computed value should be returned.

if (planet === 'Mercury')
    return earthWeight * 0.378;

Furthermore, the missing curly braces could present as a problem if JS is not able to execute the if statement.

if (planet === 'Mercury') {
    return earthWeight * 0.378;
} else
if (...) {

} ...

Are you interested in seeing the other way? Or do you want to bone up on the four concepts mentioned, first? Bookmark this topic so you can return to it in that event.

I really appreciate the explanation here. Very clear now.

I would love to see others ways to approach this but I think it’s best I bone up on those concepts first.

Thanks

1 Like