×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Javascript /Conditional Statements

Javascript /Conditional Statements

Javascript /Conditional Statements

(OP)
Hello,
Need help with my code. Do not understand why it does not work properly.
Description:
Create a web page with a linked external script that takes as input the user age. if the age is less than 18 the web page should display "You are too young to vote" Otherwise the web page should proceed by asking if the person did register to vote or not using a prompt. If No the page should display:" You need to register before you can vote" if yes the program display: " You can vote"
Use descriptive names of variables, appropriate primary and secondary conditions ( if/else), and display correctly the outputs on the webpage based on the condition.
Code:
<head>
<title> Voting! </title>
<script type="text/javascript">
var age = prompt("Please enter your age");
if(age < 18){
document.write("You are too young to vote!");
}
else if (age >=18){
alert("You are eligible to vote!");
}
var answare = prompt("Did you register to vote?");
if (answare == 'yes'){
document.write("You can vote!");}
else
{document.write("You need to register before you can vote");

}

</script>
</head>
<body>
</body>
</html>

RE: Javascript /Conditional Statements

Hi

As I understand, all you should do there is to move the entire 2nd if inside the 1st else's block.

Additional notes :
  • As a good habit is preferable to parseInt() the string values before comparison.
  • would be more appropriate to use confirm() instead of the 2nd prompt().
  • Using document.write() is generally a bad idea. To display JavaScript generated text in a document usually we use add a container tag like <div id="here-will-be-text"></div>, then set its content like document.getElementById('here-will-be-text').textContent = 'your generated text'.

CODE --> JavaScript

var age = parseInt(prompt("Please enter your age"), 10);
if (age < 18) {
    document.write("You are too young to vote!");
} else if (age >= 18) {
    alert("You are eligible to vote!");

    var answare = confirm("Did you register to vote?");
    if (answare) {
        document.write("You can vote!");
    } else {
        document.write("You need to register before you can vote");
    }
} 

Even more notes :

Feherke.
feherke.github.io

RE: Javascript /Conditional Statements

(OP)
Thank you very much!

RE: Javascript /Conditional Statements

Hi Marfa, this sounds like a homework assignment. You are hurting your career by asking someone else to do it for you. The purpose behind problems like this, is to help you learn to solve problems and provide solutions yourself. If you just pass the question off to some stranger on the internet, you aren't learning to program, you're learning to be dependent on other people. You're learning to be a bad employee.

I've worked in IT for over 40 years. I've interviewed hundreds of candidates. When people have just Googled for help to pass their classes, it shows, because it's obvious they don't really understand what they're talking about. They never make it past the first interview.

So, it's better for you if you spend a little time and effort to do it yourself, then come back here and ask a detailed question about why something didn't work as expected. That kind of homework help is ok. That's real learning.

RE: Javascript /Conditional Statements

You can learn more about JavaScript from different online websites.

<head>
<title> Voting! </title>
<script type="text/javascript">
var age = parseInt(prompt("Please enter your age"), 10);
if (age < 18) {
document.write("You are too young to vote!");
} else if (age >= 18) {
alert("You are eligible to vote!");

var answare = confirm("Did you register to vote?");
if (answare) {
document.write("You can vote!");
} else {
document.write("You need to register before you can vote");
}
}
</script>
</head>
<body>
</body>
</html>


regards
jyoti gupta

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close