Code Challenges: Javascript Fundamentals

It’s my first question in codeCademy forum. I am stuck here:

Code Challenges: Javascript Fundamentals

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

It shows the following output:

Jupiter

rather it should log

236

I can’t understand where i am missing the logic in code.

That is because you’re telling it to return planet.

const calculateWeight = (earthWeight, planet) => {
    return earthWeight = planet;
    // The function exits at the line above this one and never reaches the following code
    const weight = (calculateWeight) => {
        switch (weight) {
            case 'Mercury':
                return earthWeight * 0.378;
            case 'Venus':
                return earthWeight * 0.907;
            case 'Mars':
                return earthWeight * 0.377;
            case 'Jupitor':
                 return earthWeight * 2.36;
            case 'Saturn':
                 return earthWeight * 0.916;
            default:
                console.log('Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.');
        }
    }
}
2 Likes

Dear Thanks for your assistance, I have made changes in my code, I think in my function, before switch block, I am missing something important :

const calculateWeight = (earthWeight, planet) => {

}

I forgot to add break keyword in switch, which i added, however not getting the right answer:

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

Output:

Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.
undefined    (3 times).
const calculateWeight = (earthWeight, planet) => {
  
  switch (calculateWeight) {  //your switch condition should be planet
    case 'Mercury':
      return earthWeight * 0.378;
      break;
    case 'Venus':
      return earthWeight * 0.907;
      break;
    case 'Mars':
      return earthWeight * 0.377;
      break;
    case 'Jupitor': //Jupitor or Jupiter?
      return earthWeight * 2.36;
      break;
    case 'Saturn':
      return earthWeight * 0.916;
      break;
    default:
      console.log('Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.');
  //return 'Invalid Planet Entry...' //This would be more consistent with your previous code
      break;
  }
}
console.log(calculateWeight(100, 'Jupiter'));
console.log(calculateWeight(100, 'Mercury'));
console.log(calculateWeight(100, 'Venus'));

Your switch statement needs to test the condition that matches your case statements. You are testing which planet was supplied as an argument. Hope this helps! Happy coding!

1 Like

I was unable to understand first, Thanks for your valuable input.

One other aside: you don’t need the break keyword in your switch...case. Return will send the control flow back to the caller: console.log(calculateWeight(100, 'Jupiter'));, etc.
If you were assigning the return value to a variable, and then had only one return statement after the switch...case is finished, you would need break. For example:

const myFunction = (param1, param2) => {
  let returnValue;
  switch (param2) {
    case 'Value1':
      returnValue = param1 * 2;
      break;
    case 'Value2':
      returnValue = param1 * 3;
      break;
//more cases
    default:
      returnValue = 'Invalid Input';
      break;
  }
  return returnValue;
}

OR:

const myFunction = (param1, param2) => {
  switch (param2) {
    case 'Value1':
      return param1 * 2;
    case 'Value2':
      return param1 * 3;
//more cases
    default:
      return 'Invalid Input';
  }
}