JavaScript functions switch statements

‘use strict’;

let x = getWeight()

let y = targetPlanet()

let finalWeight = 0;

switch(y)
{
case “Mercury”: finalWeight = calcMercury(x,y);
break;

case “Venus”: finalWeight = calcVenus(x,y);
break;

case “Earth”: finalWeight = calcEarth(x,y);
break;

case “Moon”: finalWeight = calcMoon(x,y);
break;

case “Mars”: finalWeight = calcMars(x,y);
break;

case “Jupiter”: finalWeight = calcJupiter(x,y);
break;

case “Saturn”: finalWeight = calcSaturn(x,y);
break;

case “Uranus”: finalWeight = calcUranus(x,y);
break;

case “Neptune”: finalWeight = calcNeptune(x,y);
break;

case “Pluto”: finalWeight = calcPluto(x,y);
break;
}

outdata(finalWeight,y);

function getWeight () {

let weight = Number(prompt(‘Enter your current weight’));

return weight;

}

function targetPlanet() {

let planet = prompt(‘Enter a solar planet’);

return planet;

}

function calcMercury(weight,planet) {

let mercury = 0.378;

if(planet===‘Mercury’) {

let newWeight = weight * mercury;

return newWeight;

}else{

}

}

function calcVenus(weight,planet) {

let venus = 0.907;

if(planet===‘Venus’) {

let newWeight = weight * venus;

return newWeight;

} else{

}

}

function calcEarth(weight,planet) {

let earth = 1;

if(planet===‘Earth’) {

let newWeight = weight * earth;

return newWeight;

} else{

}

}

function calcMoon(weight,planet) {

let moon = 0.166;

if(planet===‘Moon’) {

let newWeight = weight * moon;

return newWeight;

} else{

}

}

function calcMars(weight,planet) {

let mars = 0.166;

if(planet===‘Mars’) {

let newWeight = weight * mars;

return newWeight;

} else{

}

}

function calcJupiter(weight,planet) {

let jupiter = 2.36;

if(planet===‘Jupiter’) {

let newWeight = weight * jupiter;

return newWeight;

} else{

}

}

function calcSaturn(weight,planet) {

let saturn = 0.916;

if(planet===‘Saturn’) {

let newWeight = weight * saturn;

return newWeight;

} else{

}

}

function calcUranus(weight,planet) {

let uranus = 0.889;

if(planet===‘Uranus’) {

let newWeight = weight * uranus;

return newWeight;

} else{

}

}

function calcNeptune(weight,planet){

let neptune = 0.071;

if(planet===‘Neptune’) {

let newWeight = weight * neptune;

return newWeight;

} else{

}

}

function calcPluto(weight,planet) {

let pluto = 0.071;

if(planet===‘Pluto’) {

let newWeight = weight * pluto;

return newWeight;

} else{

}

}

function outdata(newWeight,planet){

alert('Your weight on ’ + planet + ’ is ’ + newWeight +"!");

}

CAN SOMEONE HELP ME AND PLEASE TELL ME WHERE I WENT WRONG AT. WHEN I RUN MY CODE THE WEIGHT FOR EACH PLANET RETURN AS 0 WHY IS THAT?

Which lesson is this for?

I am learning Javascript. They want me to ask a user to enter a weight and a planet return both calculation and print a output message to user with the information

There is a concept you will have people teach you in coding over and over, and I think we should brush over that first. You want to write DRY code. It stands for don’t repeat yourself. For each planet you write the same function over and over. If you catch yourself doing that ever, then you’re likely doing the thing wrong. Instead write the equivalent of that function once, and either use an if/elseif/else or switch statement to change numbers for output. For example, here’s my rendition of that:

function calculateWeight(earthWeight, planet) {
  if(isNaN(earthWeight)){
	return "Please enter a number for earthWeight.";
 }
  if(typeof planet != 'string'){
    return "Please enter a valid 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(195, "Jupiter")) // You can test things out here with a few variations if you like. I left out all the prompts, alerts, etc. You can write those on your own.

I’ll poke around with the code you already have too and see if I can figure out what you’re doing wrong.

Try this for your user prompts:
const planet = prompt(“Please enter a planet name”, “Earth”);
To break this down a little:
let/const variableName = prompt(“message to user”, “default option if you desire”);
You are creating a whole function around getting input through prompts.

isNaN probably doesn’t do what you think. you could test the type instead, if that’s what you meant.

speaking of repetition, this could be taken away too:

  return earthWeight
} else if (planet ==
  return earthWeight
} else if (planet ==
  return earthWeight
} else if (planet ==
  return earthWeight
} else if (planet ==
  return earthWeight

and instead keeping only the unique things

{
  "Mercury": 0.378,
  "Venus":   0.907,
  "Mars":    0.377,
  "Jupiter": 2.36,
  "Saturn":  0.916,
}
1 Like

If I remember right, the lesson he is on comes before anything about object literals, so he won’t know how to use that part yet. I think with only conditionals, loops, and functions learned - lacking objects and object literals - what I posted is the best solution. I’m not super experienced though. If what you’re referring to doesn’t require objects, break it down further for me please so I can understand.

  • Ask the user to enter a weight and return the number they entered.
  • Ask the user to enter a planet and return the name of the planet they entered.
  • Calculate the weight of the user on the other planet and return the new weight.
  • Print an output message to the user in an alert.

Yeah that’s fair. Mainly my concern was with isNaN which does something other than what you’re using it for.

1 Like