0

I want it asked "What is your age?" If the age is < 21 I want the alert that follows and the page to close right after. In this case it still brings up the "Age doesn't match" alert. Been coding for 1.5 hrs, don't make fun of me.

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

if (age < 21) {
   alert("Access denied");  window.close();
}

if (age == 21) {
   alert("Just made it");
}

if (age > 21) {
   alert("Welcome aboard");
}
 // Confirm

if (age >= 21) {
var ageconfirm = prompt("Again?");
}

if (age==ageconfirm) {
   alert("Just checkin'")
}
if (age != ageconfirm) {alert("Age doesn't match"); 
}
if (ageconfirm <21) {  window.close(); 
}
C Jackson
  • 3
  • 1

3 Answers3

0

This should do what you want it do to:

var age, ageconfirm;
age = prompt("What is your age?");

if (age < 21) {
   alert("Access denied");  window.close();
} else if (age == 21) {
   alert("Just made it");
} else if (age > 21) {
   alert("Welcome aboard");
}
// Confirm

if (age >= 21) {
    ageconfirm = prompt("Again?");
    if (age == ageconfirm) {
        alert("Just checkin'");
    } else {
        alert("Age doesn't match");
    }
} else {
    window.close();
}

One thing to keep in mind is that prompt returns a string so it might be a good idea to use parseInt. Then you use can === to compare instead of ==.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • Thanks a bunch. You made my code much cleaner in 20 seconds! I tried the return option, and I felt I used it properly, but it just made my code not run. I have much to learn, i just hit my first roadblock with this and could find a good solution. – C Jackson Mar 24 '17 at 15:03
0

You can use return to stop execution:

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

if (age < 21) {
   alert("Access denied");
   return window.close();
}

if (age == 21) {
   alert("Just made it");
}

if (age > 21) {
   alert("Welcome aboard");
}

// Confirm

if (age >= 21) {
    var ageconfirm = prompt("Again?");
}

if (age  == ageconfirm) {
   alert("Just checkin'")
}
if (age != ageconfirm) {
    alert("Age doesn't match"); 
}
if (ageconfirm < 21) {
    return window.close(); 
}
BenShelton
  • 901
  • 1
  • 8
  • 20
0

The problem is your second block of code should be nested within that if block. At the moment you say 'If the user is over 21, prompt them to enter their age again.' Then you exit that if block, so regardless of what their original answer is you execute the last code.

var age, ageconfirm

age = prompt("What is your age?");

if (age < 21) {
   alert("Access denied");  window.close();
}
else if (age == 21) {
   alert("Just made it");
}
else if (age > 21) {
   alert("Welcome aboard");
}

// Confirm
if (age >= 21) {
    ageconfirm = prompt("Again?");

  if (age == ageconfirm) {
     alert("Just checkin'")
  }
  else if (age != ageconfirm) {
    alert("Age doesn't match"); 
  }
  else if (ageconfirm < 21) {  
    window.close(); 
  }
}

Here's a JSFiddle of my solution to check:

https://jsfiddle.net/00wcbaox/2/

JCollerton
  • 3,227
  • 2
  • 20
  • 25
  • I know I wasn't using everything properly, but even in the solution you have it sill brings up the "Age doesn't match" alert if under 21. Appreciate the response all the same – C Jackson Mar 24 '17 at 15:07
  • Have you tried that JSFiddle? I don't understand why that wouldn't work. – JCollerton Mar 24 '17 at 15:25
  • Yes, and thanks for inadvertently showing me cool resource. But if you type in, say 20, it says access denied and then brings up a second alert which is what I was trying to stop. – C Jackson Mar 24 '17 at 15:29
  • Ahh. misunderstood what you were asking! I've amended it above, sorry about that :). – JCollerton Mar 24 '17 at 16:39