Ask coding questions

← Back to all posts
JS Newbie.....dont laugh
isreal (1)

Figure its something simple thats wrong but I haven't figured it out. Any guidance is greatly appreciated!

function isOldEnoughToDrink(age) {

}
var age=22;
var legalAge=21;
var theDrinker=age;

if (theDrinker>=legalAge){
console.log(true);
}else{
console.log(false);

}

Answered by Zavexeon (669) [earned 5 cycles]
View Answer
Commentshotnewtop
Zavexeon (669)

I made some edits.

First of all, your code to check the age is outside of the function, it should be on the inside. You don't need to declare age either, you just need to call the function like this, isOldEnoughToDrink(ageHere);.

Fixed code:

function isOldEnoughToDrink(age) {
  var legalAge = 21; //'const legalAge = 21' is preferred 
  var theDrinker = age;
  
  if (theDrinker >= legalAge) {
      console.log(true);
  } else {
      console.log(false);
  }
}

isOldEnoughToDrink(22);
ash15khng (473)

What's the question and the intended output?

barrycarter (12)

It seems to work and print true (ignore the undefined that comes after it). Is that not what you expected?