Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript /Conditional Statements

Status
Not open for further replies.

Marfa

Technical User
Oct 12, 2020
2
US
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>
 
Hi

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

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

JavaScript:
[b]var[/b] age [teal]=[/teal] [COLOR=orange]parseInt[/color][teal]([/teal][COLOR=orange]prompt[/color][teal]([/teal][i][green]"Please enter your age"[/green][/i][teal]),[/teal] [purple]10[/purple][teal]);[/teal]
[b]if[/b] [teal]([/teal]age [teal]<[/teal] [purple]18[/purple][teal]) {[/teal]
    document[teal].[/teal][COLOR=orange]write[/color][teal]([/teal][i][green]"You are too young to vote!"[/green][/i][teal]);[/teal]
[teal]}[/teal] [b]else if[/b] [teal]([/teal]age [teal]>=[/teal] [purple]18[/purple][teal]) {[/teal]
    [COLOR=orange]alert[/color][teal]([/teal][i][green]"You are eligible to vote!"[/green][/i][teal]);[/teal]

    [b]var[/b] answare [teal]=[/teal] [COLOR=orange]confirm[/color][teal]([/teal][i][green]"Did you register to vote?"[/green][/i][teal]);[/teal]
    [b]if[/b] [teal]([/teal]answare[teal]) {[/teal]
        document[teal].[/teal][COLOR=orange]write[/color][teal]([/teal][i][green]"You can vote!"[/green][/i][teal]);[/teal]
    [teal]}[/teal] [b]else[/b] [teal]{[/teal]
        document[teal].[/teal][COLOR=orange]write[/color][teal]([/teal][i][green]"You need to register before you can vote"[/green][/i][teal]);[/teal]
    [teal]}[/teal]
[teal]}[/teal]

Even more notes :
[ul]
[li]This is pure JavaScript question, so is off-topic here as belongs to forum216.[/li]
[li]This looks like a homework question from Get questions and answers for Computer Science, which are forbidden on this site.[/li]
[/ul]


Feherke.
feherke.github.io
 
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.
 
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top