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

170
Visualizações
Code platoon conditional statements problem set

Getting my coding learning path started and I've run across a hiccup. I'm trying to write a program that represents the below;

// Write a program that prints a message to the screen based on a users age and country.

// Feel free to change these variables or create new ones so you can test all cases.

const age = 20

const country = 'USA'

// if the user is younger than 16 print "You're not old enough to do anything yet."

// if the user is at least 16 but not yet 18 print "Be careful driving."

// if the user is 18 but not yet 21 and the user lives in the USA pring "Go Vote!"

// if the user is at least 18 but younger than 21 and lives outside of the US print "You can probably have some wine."

// In all other cases print "You're old enough to figure it out for yourself."

So far ive got this:

const age = 20

const country = "USA"

const otherCountry = "Other country"

console.log(age)

console.log(otherCountry)


if (age < 16 ) {
  console.log("You're not old enough to do anything.") 
  }
  else if (age >=16 && age <=18) {
    console.log ("Be careful driving")
    }
    else if (age >=18 && age <=21 && country) {
      console.log("Go Vote!")
    }
    else if (age >=18 && age <=21 && otherCountry){
      console.log ("You can prbably have some wine")
    }
    else
    {console.log ("You're old enough to figure it out")
    }

        

I cant seem to figure out how to get the country to be expressed in the "else if" statement. Noobies got to start somewhere. Thanks in advance

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have a problem with your logic.

First of all not yet 18 probably means < 18 and not <= 18. (same for 21)

Second, once you are in your "Go vote" branch, you can never enter the "Wine" branch anymore, because once, one condition is hit in an if .. else if .. else no other conditions will be evaluated any more.

So, if you have a person between 18 and 21, you have to check the addtional condition (ie lives in USA or any other country) in that branch.

let age = 19;
let livesin = "USA";

if (age < 16 ) {
  console.log("You're not old enough to do anything.") 
}
//you don't need >=16 here, because as the first condition failed
//we alread know that age >= 16
else if (age < 18) {  
  console.log ("Be careful driving")
}
//you don't need >=18 here, because as the first and second condition failed
//we already know that age >= 18
else if (age <21) {
  //for people between 18 and 21, check if they live in the USA or not
  if (livesin === "USA") console.log("go vote");
  else console.log("You can prbably have some wine")
}
else {
  console.log ("You're old enough to figure it out")
}
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