/    Sign up×
Community /Pin to ProfileBookmark

Javascript /need help with Conditional Statements

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:

[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>
[/code]

Report

Edit

Delete

**(Added `[code]…[/code]` tags ~ MOD)**

to post a comment
HTMLJavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@daveyerwinOct 13.2020 — @Marfa#1624173

Your problem is the script continues to run after

a number less than 18 is entered into the first prompt.

The script as written has no way to be stopped before

running all the code so the second prompt is displayed.

One way to solve this is to put your code into a function

``<i>
</i>function start(){
var age = prompt("Please enter your age");
if(age &lt; 18){
document.write("You are too young to vote!");return;
}
if (age &gt;=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");
}<i>
</i>
`</CODE>
Now you have a new problem, the function must be called in order to<br/>
run the code. this can be done by user action such as clicking a button ...

<CODE>
`<i>
</i>&lt;head&gt;
&lt;title&gt; Voting! &lt;/title&gt;
&lt;button onclick = start()&gt;Start the code&lt;/button&gt;
&lt;script type="text/javascript"&gt;
function start(){
var age = prompt("Please enter your age");
if(age &lt; 18){
document.write("You are too young to vote!");
return;
}
if (age &gt;=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");
}
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;<i>

</i>
``

the return statement will end execution of the function if a number

less that 18 is entered in the first prompt
Copy linkTweet thisAlerts:
@rpg2009Oct 13.2020 — Another option would be to use an [immediately invoked function expression](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) (IIFE) or in English a function that runs itself

``<i>
</i>(function () {
const age = window.prompt('Please enter your age')

if (age &lt; 18) {
document.write('You are too young to vote!')
return
}

// no need for if (age &gt;= 18) we dealt with that condition above
window.alert('You are eligible to vote!')

const answer = window.prompt('Did you register to vote?')

if (answer === 'yes') document.write('You can vote!')
else document.write('You need to register before you can vote')
}())<i>
</i>
`</CODE>
edit: in fact if we wanted to be consistent we could remove the else conditions entirely and just rely on early returns

<CODE>
`<i>
</i>(function () {
const age = window.prompt('Please enter your age')

if (age &lt; 18) {
document.write('You are too young to vote!')
return // leave the running function
}

window.alert('You are eligible to vote!')

const answer = window.prompt('Did you register to vote?')

if (answer === 'yes') {
document.write('You can vote!')
return // leave the running function
}

document.write('You need to register before you can vote')
}())<i>
</i>
``


Note: for learning conditionals document.write serves a purpose here. Generally it is something to be avoided though.
×

Success!

Help @Marfa spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...