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

169
Vistas
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 Respuestas
Responde la pregunta

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 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