FAQ: Code Challenges: JavaScript Fundamentals - calculateWeight()

Should that be a number? Or are you checking is earthWeight has a value set? It can be 1 or 100 meaning 1kg on earth, or 100lb, or 100 tonnes. It’s all relative.

When I wrote the parameter planet before my parameter earthWeight for my function it didn’t work in my switch statement. Once I swapped the parameters the function worked but and I am wondering why? :thinking:

Here is an example of the first way I wrote it which didn’t work:

let calculateWeight = (planet, earthWeight) => {
switch (planet)

If the function is taking multiple parameters why does the order I put them in matter?

Below is the correct way to write the function.

let calculateWeight = (earthWeight, planet) => {
switch (planet)

Thanks in advance for your help!

the exercise has some test cases/function calls where the arguments are in a certain. And given parameter and arguments are positional, order matters

Hi there,

i want to make my function has some line that checks given arguments datatype.

Theres a couple of ways you could do this:

As you know what the user wants to input you could create an if statement to check through all the possible entires I.e “Venus” || “Saturn” || Mars but you’re on the right track with typeof.

if (typeof earthWeight !== "number" || typeof planet !== "string"){
    return "Invalid data type";

this will check if the data type of earthWeight is a number and that the data type of planet is a string.

If it’s not you can return an error message. Once that’s working you can then create a switch statement and have the default return another error message if the string input isn’t valid.

default:
     return "Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.";

Hope this helps!

My code is as below. I run it and it works as it should. However the codecademy system doesn’t accept it as an answer i.e didnt get the green tick. What’s wrong with the code? I got this error message:

If the planet passed as an argument is anything other than the Mercury, Venus, Mars, Jupiter or Saturn, your function should return: ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’ : expected ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter or Saturn.’ to equal ‘Invalid Planet Entry. Try

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

same as:

Hi all.

I wanted to chime in here, in case anyone else (like me) isn’t able to figure out why their code isn’t working.
I used if/else to solve this one and my problems were-

  1. trying to insert a string to give a better output:
    if (planet === ‘Mercury’) {
    return 'Weight on planet Mercury is ’ + earthWeight * 0.378; …

  2. adding a zero to some of the numbers (0.378 instead of
    .378).

After I removed the above-mentioned things my code passed.

@stetim94 hinted on another post about my first issue, and that helped me with the second.

Question about my code here, the noted version of the beginning if/else statement to find invalids works and allows me to move on, but the second returns as undefined. Why? any help would be greatly appreciated.

Thanks

const calculateWeight = (earthWeight, planet) => {
/if (planet === ‘Mercury’ || planet === ‘Venus’ || planet === ‘Mars’ || planet === ‘Jupiter’ || planet === ‘Saturn’) {}
else {
return ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’;
}
/
if (planet === !‘Mercury’ || planet === !‘Venus’ || planet === !‘Mars’ || planet === !‘Jupiter’ || planet === !‘Saturn’) {
return ‘Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.’;
}
else if (planet === ‘Mercury’){
return earthWeight * 0.378;
}
else if (planet === ‘Venus’){
return earthWeight * 0.907;
}
else if (planet === ‘Mars’){
return earthWeight * 0.377;
}
else if (planet === ‘Jupiter’){
return earthWeight * 2.36;
}
else if (planet === ‘Saturn’){
return earthWeight * 0.916;
}
}
console.log(calculateWeight(100, ‘Jupitr’))

if a === NOT 'string'

The NOT is evaluated first since it has higher precedence than identity so the comparison will be with false, not the string itself.

The correct form of NOT EQUAL TO is,

!==
1 Like

Why am I getting an error on the last else statement? in the previous exercise I changed it to else if and it passed, which confused me.

Strings returned need to be an exact match, so I would copy the string from the instructions.

const calculateWeight = (earthWeight, planet) => {
  if (planet === 'Mercury') {
    return earthWeight * 0.378;
  } else if (planet === 'Venus') {
    return earthWeight * 0.907;
  } else if (planet === 'Mars') {
    return earthWeight * 0.377;
  } else if (planet === 'Jupiter') {
    return earthWeight * 2.36;
  } else if (planet === 'Saturn') {
    return earthWeight * 0.916;
  } else if (planet === 'Pluto') {
    return earthWeight * 2.789;
  } else {
   return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, Pluto, or Saturn.';
 }
};
console.log(calculateWeight(275, 'Pluto'));
console.log(`You would weigh ${calculateWeight} on ${planet}!`);

I already completed the task but wanted to add more. I’m having trouble with the console.log part, I would like it to log "You would weigh calculateweight on planet. "
but I just can’t seem to figure it out, I tried interpolation string and got error. what am I doing Wrong here?

I’m guessing the output you’re getting looks something like this…

You would weigh [Function function] on undefined!

That’s because you have not invoked the function like you did in the line above that one.


Nope. Just checked. It gives a reference error for planet. Anyway, easy fix.

Yeah reference error but how would I fix that?
Been stuck for about 45 minutes.

planet only exists inside the function. If you wish to use a variable in that string then declare it in the same scope as the call to the function, or log the outcome inside the function.

The lesson checker is expecting a number as the return value.

1 Like

I did something different myself, I think there’s no need to create another variable. Just defining planet to lowerCase will do the trick!

function calculateWeight (earthWeight, planet){
planet = planet.toLowerCase()
if (‘mercury’=== planet){
return earthWeight *0.378

}

Hello,

I have completed this exercise, the code works, but I can not move forward to the next exercise. I got this message “When the planet is Mercury, the function should return the weight passed in multiplied by .378”, but the fuction returns the weight multiplied by 0.378 just like in the text. What could be the problem ?

// Write your function here:

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

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

`${planet} weight = ` + `${earthWeight}` * 0.378;

The math expression should be wholly contained in the template expression. Also consider writing one complete string without concatenation…

    `${planet} weight = ${earthWeight * 0.378}`;

Another consideration is what the SCT is expecting as a return value from the function. What do the instructions ask for?

Thank you for your info,

I have rewrite the code using the switch statement, and now works.

Hi. I’m trying to do a different approach to this challenge. I know how to do it with conditionals but i’m trying to think outside the box. Please review and feel free to provide any feedback. Below is my code. It’s hitting the first conditional as opposed to the else statement. Thanks !!

const calculateWeight = (num,str) => {
  let planets = ["Mercury", "Venus", "Mars", "Jupiter", "Saturn"]

  for(let i = 0; i < planets.length; i++) {

    let weight;

    // if(str !== planets[i]){
    //   return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
    // }

    if(planets[i] === "Mercury") {
      weight = num * 0.378
      return weight
    } else if (planets[i] === "Venus") {
      weight = num * 0.907
      return weight
    } else if (planets[i] === "Mars") {
      weight = num * 0.377
      return weight
    } else if (planets[i] === "Jupiter") {
      weight = num * 2.36
      return weight
    } else if (planets[i] === "Saturn") {
      weight = num * 0.916
      return weight
    } else {
      return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
    }
  }
}


console.log(calculateWeight(10, "Moon"))