Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
How to do string interpolation with function with two parameters javaScript

I am a complete newbie. But I am working on a piece of training code and hit a wall. I completed the project, however, I wanted to test an additional piece of code. I want to use string interpolation to show the user a message like this:

If this object were on ${planetName} it would weight ${thisMuch}.

But I am confused on how to call only one argument from a function with two parameters.

function calculateWeight(earthWeight, planet) {
    let weight;
    if(planet === 'Mercury') {
    weight = earthWeight * .378
    return `${weight}`
    }if(planet === 'Venus') {
    weight = earthWeight * .907
    return `${weight}`
    }if(planet === 'Mars') {
    weight = earthWeight * .377
    return `${weight}`
    }if(planet === 'Jupiter') {
    weight = earthWeight * 2.36
    return `${weight}`
    }if(planet === 'Saturn') {
    weight = earthWeight * 0.916
    return `${weight}`
    } else {
    return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
       }
    }
    
    
    // Uncomment the line below when you're ready to try out your function
     console.log(calculateWeight(100, 'Jupiter')) 

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Couple of changes :

First there is a difference in the way you have written multiple ifs and if, else-if. The first one will run all planet conditions even if the 1st one is satisfied. You do not want that.

Second, try to return your statement from inside the function. You are returning only weight for a planet.

With the statement in place all you need is the weight value and your job can be done with a single return statement.

function calculateWeight(earthWeight, planet) {
let weight;
if(planet === 'Mercury') {
weight = earthWeight * .378
}else if(planet === 'Venus') {
weight = earthWeight * .907
}else if(planet === 'Mars') {
weight = earthWeight * .377
}else if(planet === 'Jupiter') {
weight = earthWeight * 2.36
}else if(planet === 'Saturn') {
weight = earthWeight * 0.916
} else {
return 'Invalid Planet Entry. Try: Mercury, Venus, Mars, Jupiter, or Saturn.'
   }
return `If this object were on ${planet} it would weight ${weight}.`
}


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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda