How earthWeight declared as a number?

How earthWeight parameter should be declared as a number?.

It can be a number, 1, or you could use your own weight in pounds or kilograms to see what you would weigh on the other planets.

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'));

This is my code. All running smooth, Got the o/p as well. Still instructions guide is not accepting and shows wrong.

How are you checking if the earth weight is a number?

It is mentioned in the instructions and inside parameter i’m calling!?

We don’t see a check of the earthWeight value/type. We need some mechanism at the top of the function that catches invalid data type.

Such like creating it with value, let earthWeight = 100; like this?

We need to be sure that a valid number is passed in, when it is out of our control, such as user input.

if (typeof earthWeight !== 'number') {
    return // error message
}

The actual value we give to earthWeight is not assigned outside of the function. It is in the function call argument list…

console.log(calculateWeight('one', 'Jupiter'));

That should return,

Invalid Earth weight value

But, the problem gets trickier when we pass what looks like a number, but is actually a string.

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

The number is in quotes. That means checking the data type is more or less a clunky solution. Have you investigated any of the JavaScript Number functions?

Two that come to mind in this particular situation are parseInt() and parseFloat(). Definitely read up on them.

console.log(parseFloat(NaN))
console.log(parseFloat(true))
console.log(parseFloat(false))
console.log(parseFloat('100'))
console.log(parseFloat('one'))
console.log(parseFloat(`${Math.PI}`))
NaN
NaN
NaN
100
NaN
3.141592653589793

We can use the two methods in an if, while, switch and ternary, and anywhere else we want to ensure we are indeed working with a number, even if it is just to coerce a string, like, '100' to an actual number.

if (! parseFloat(earthWeight)) {
    return // error message
}

If the value makes it past this test, then to convert it to a number just multiply by 1

earthWeight *= 1

or, use the unary operator mentioned below…

There is another way, perhaps not as thorough as the above since it lets true pass as a coerced, 1, and that is the unary operator, +.

if (! +earthWeight) {
    return // error message
}
2 Likes

test your code with each of the different planets including the default