Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

233
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda