Home > Back-end >  Very new to Javascript and html and programming in general Y/N if problem
Very new to Javascript and html and programming in general Y/N if problem

Time:12-03

I made a very simple code to pop up two questions, ask about age and if you have drivers license. What i want it to do is to answer the following: over 21 and Y = you can drive; under 21 and Y = you can drive; under 21 and N = you can't drive; over 21 and N = you can't drive.

The problem here is in the case of under 21 and Y because it says you can't drive, I have tried with || and && and two ifs, and the problem is different but similiar in one case.

<meta charset="UTF-8">

<script>
    function skipLine() {
        document.write("<br>");
    }

    function print(phrase) {
        document.write(phrase);
        skipLine();
    }

    var age = parseInt(prompt("What's your age?"));
    var haveLicense = prompt("Do you have drivers licence Y or N");

    if((age >= 21) && (haveLicense == "Y")) {
            print("You can drive");
        }

    else {
        print("You can't drive");
    }

</script>

CodePudding user response:

function skipLine() {
  document.write("<br>");
}

function print(phrase) {
  document.write(phrase);
  skipLine();
}

var age = parseInt(prompt("What's your age?"));
var haveLicense = prompt("Do you have drivers licence Y or N");

if (haveLicense == "N") {
  print("You can't drive");
} else {
  print("You can drive");
}

CodePudding user response:

Ohhh you are 100% right, thank you

  • Related