Home > Enterprise >  How can I call a function within another function and make it run an if statement with a variable?
How can I call a function within another function and make it run an if statement with a variable?

Time:09-11

How can I make it so that when I type in the prompt, the value is stored as a number and refers to the second function?

function driversAge()

    {
    validation();
    var age = prompt("What is your age?");
    }

function validation()

    {
    if(Number(age) === 18){
    alert("You are 18");
    } 

    else if(Number(age) < 18){
    alert("you are below 18");
    } 
    else if(Number(age) > 18){
    alert("You are over 18");
    }
    }

    driversAge();

CodePudding user response:

You could have your driversAge function return the value, then pass it to your validation function.

function driversAge () {
  return prompt('What is your age?')
}

function validation(age) {
  if (age > 18) {
    alert("Over 18")
  }
  else {
    alert("Not over 18");
  }
}

const age = driversAge();
validation(age);

  • Related