0

The first Prompt is working. The second is not working. the second function supposed to check if email contains @

I tried to switch the email function place

<body onload="check_user_age(); check_email" style="position:absolute">
    <h1>Spiritueux Wines and Liquors</h1>
    <script>
        function check_user_age() {
            if (age_of_user() < 18)
                alert("You are too young to buy alcohol.");
        }

        function age_of_user() {
            var age = prompt("What is your age?");
            return age;
        }


        function check_email() {
            if (email.includes("@"));
            alert("Your email is correct");
        }

        function email_of_user() {
            var email = prompt("what is your email");
            return email;
        }

    </script>
</body>
2
  • 1
    Well they are coded differently and you do not call check_email Commented Aug 23, 2019 at 19:46
  • There's a stray semi-colon at the end of the if statement, which causes the body of the if to be empty. Commented Aug 23, 2019 at 19:46

2 Answers 2

3

First you do not call the method

onload="check_user_age(); check_email"
                                   ^^^ not executed

Second you have a semicolon after the if

if (email.includes("@"));
                       ^^^

Third, email is not defined

if (email.includes("@"));
    ^^^^^
Sign up to request clarification or add additional context in comments.

Comments

0

First, in the onload event, you are not calling the function. Second, in check_email(), you are giving semicolon which is making if body empty. Third, you are checking the condition with undefined variable. Because of these reasons, you are not getting output. You can write like these:

function check_user_age() {
  if (age_of_user() < 18)
    alert("You are too young to buy alcohol.")
  else
    alert('Welcome to Spiritueux Wines and Liquors')
}
function age_of_user() {
  var age = prompt("What is your age?");
  return age;
}
function check_email() {
  if (email_of_user().includes("@"))
    alert("Your email is correct");
  else
    alert('Your email is incorrect')
}

function email_of_user() {
  var email = prompt("what is your email");
  return email;
}
<body onload="check_user_age(); check_email()" style="position:absolute">
    <h1>Spiritueux Wines and Liquors</h1>
  </body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.