No need to. It was declared in the parameter list.
Greetings!
While solving this exercise I came up with this code:
function calculateWeight(earthWeight, planet) {
if (isNaN(earthWeight) || earthWeight < 0) {
return 'Provide a valid weight.'
} else {
switch (true) {
case (planet === 'Mercury'): return `The weight in ${planet} is ${earthWeight*0.378}`;
case (planet === 'Venus'): return `The weight in ${planet} is ${earthWeight*0.907}`;
case (planet === 'Mars'): return `The weight in ${planet} is ${earthWeight*0.377}`;
case (planet === 'Jupiter'): return `The weight in ${planet} is ${earthWeight*2.36}`;
case (planet === 'Saturn'): return `The weight in ${planet} is ${earthWeight*0.916}`;
default: return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
}
}}
Which then I tested with:
console.log(calculateWeight(100, ‘Mercury’));
returning ‘The weight in Mercury is 37.8’. However, CodeAcademy provides this error message:
“When the planet is Mercury, the function should return the weight passed in multiplied by .378”,
which is clearly what I am doing. Am I missing something?
Thank you in advance!
Two things, have you tried returning just the computed value with no verbiage; and, could your switch be a little neater if the switch expression was simply, planet
and the case expressions just the planet names. It won’t make any difference but simpler is easier to read and maintain.
switch (planet) {
case 'Mercury': return earthweight * 0.378;
...
}
Thank you very much for the quick response and the suggestion! All those ‘planets’ were really cluttering the space. Something odd is that just changing that was enough for the Code Academy algorithm to read correctly the returned value, so problem solved! Many thanks one again!
I made this exact mistake, and it was completely crushing all the confidence I had with if/else statements because I felt like I had done very well up to this point. I thought maybe I should change the “=” to “===” but I second guessed myself and drove myself crazy jumping around on forums trying to find an answer.
sweet relief!
I chose If/else for this lesson. at first I had a problem with the code not running correctly due to not typing operators correctly, but now everything is running correctly. I get the needed answer, I don’t receive any error code for the string or anything else but it will not let me proceed. is there something in this that I’m missing, or is Switch statement mandatory to proceed? maybe its broke and its not displaying the error for me and I need to refresh my page?
Why does this return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
? Wouldn’t weight
and planet
be considered variables with block scope? I put console.log(planet)
into every case, and it only prints on default, and considering that planet
is an exact match of the 'Jupiter'
case, it would make sense to me to have it pass the case, ESPECIALLY since writing console.log((planet === 'Jupiter'))
prints true
. It did work when I returned the weight on every case, and that is extremely boggling to me. Can someone explain this and why I can’t condense the code like this?
there are no break
statements anywhere, so even when a case is true, it will still apply the default afterwards
furthermore, the return statement seems to be part of the default case
Why does it apply the default afterwards? Additionally, where would I even put the break
statement?
How is the return statement part of the default case, also? It’s not in the same block. I think I figured that one out, actually.
there are no break
's, as a result, the code walk through all the cases (including the default) instead of stopping after having found a match
more about switch and break here
I got it! Thank you so much!
For outside readers with this same question, I just called the function like calculateWeight(earthWeight, planet)
by itself, removed the weight
variable, and wrote console.log(earthWeight * [number])
followed by break
in every case. Like so:
case "Jupiter" :
console.log(earthWeight * 2.36);
break;
Can someone please tell me why I’m getting “undefined” printed to console? Here’s my code:
function 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, ‘Mars’))
It’s paramount that the return expression be on the same line as the keyword.
Got it! Thank you so much.
I tried to randomize my weight and planet entries, however when running my code I get a Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn. error. Can anyone tell me what I’m doing wrong?
// Planet randomizer
const randomPlanet = () => {
const randomNumber1 = Math.floor(Math.random() * 5)
switch (randomNumber1) {
case 0:
return 'Mercury'
break;
case 2:
return 'Venus'
break;
case 3:
return 'Jupiter'
break;
case 4:
return 'Saturn'
break;
case 1:
return 'Mars'
break;
}
}
//Weight calculator
const calculateWeight = (earthWeight, planet) => {
switch (planet) {
case 'Mercury':
return earthWeight * .378
case 'Venus':
return earthWeight * .907
case 'Mars':
return earthWeight * .377
case 'Jupiter':
return earthWeight * 2.36
case 'Saturn':
return earthWeight * .916
default:
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
}
}
//Number randomizer
const randomNumber = Math.floor(Math.random() * 100)
//Prints
console.log(randomPlanet())
console.log(randomNumber)
console.log(calculateWeight(randomNumber,randomPlanet))
I still can’t imagine this solution! I am just at the beginning of my path, Hopefully I’ll get enough knowledge to write something similar…
Meanwhile I am stuck with this. Solution was simple but I was wondering if I could use Ternary. So far ain’t working.
/*
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.';
}
}
*/
const calculateWeight = (earthWeight, planet) => {
planet = 'Mercury' ? true : false;
if (false) {
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.';
} else {
return earthWeight * 0.378;
}
}
console.log(calculateWeight(100, 'Mars'));
//It does log... 3.7800000000000002
Thanks.
Ive come out with this but i am afraid im doing something totally wrong…
function calculateWeight(earthWeight, planet) {
return ((planet) === 'Mercury' ? (earthWeight * 0.378) : ('Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'));
}
console.log(calculateWeight(100, 'Mars'));
This is one time when a ternary is not a very good tool to reach for. Better to use if…else if…else, or a switch.
return planet === 'Mercury' ? earthWeight * 0.378 :
planet === 'Venus' ? earthWeight * 0.907 :
planet === 'Mars' ? earthWeight * 0.377 :
planet === 'Jupiter' ? earthWeight * 2.36 :
planet === 'Saturn' ? earthWeight * 0.916 :
'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
While it may look elegant, it turns out this is hugely discouraged for a number of reasons, chief among them being it cannot be ported to another language in this form. It is easy to make mistakes and hard to spot them, and the repetition is a strike against it.
switch (planet) {
case 'Mercury: a = 0.378; break;
case 'Venus': a = 0.907; break;
case 'Mars': a = 0.377; break;
case 'Jupiter': a = 2.36; break;
case 'Saturn': a = 0.916; break;
default: return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
}
return a * earthWeight
Have you studied objects yet? There is a method using one of them, as well.
Thanks. Thanks a lot.