Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!