Input age of the person and we have to write a function to validate whether person is eligible for voting or not in JavaScript?
输入人员的年龄,我们必须编写一个函数来验证人员是否有资格使用JavaScript投票?
Code 1: Input age and print message in body section
代码1:在正文部分输入年龄和打印消息
<html lang="en">
<head>
<script>
function validate(age){
var ans="not eigible";
if(age>=18){
ans="eligible";
}
return(ans);
}
</script>
</head>
<body>
<script>
var age=parseInt(prompt("Enter age"));
var status=validate(age);
document.write("You are <b>"+status+"</b> for Vote");
</script>
</body>
</html>
Code 2: Input age in body section and print the message through function
代码2:在正文部分输入年龄并通过功能打印消息
<head>
<script>
function validate(age){
var ans="not eigible";
if(age>=18){
ans="eligible";
}
document.write("You are <b>"+ans+"</b> for Vote");
}
</script>
</head>
<body>
<script>
var age=parseInt(prompt("Enter age"));
validate(age);
</script>
</body>
</html>
Code 3: Input age from the function and print the message in body section
代码3:从功能中输入年龄并在正文部分显示消息
<html lang="en">
<head>
<script>
function validate(){
var age=parseInt(prompt("Enter age"));
var ans="not eigible";
if(age>=18){
ans="eligible";
}
return(ans);
}
</script>
</head>
<body>
<script>
var status=validate();
document.write("You are <b>"+status+"</b> for Vote");
</script>
</body>
</html>
Code 4: Input age from the function and printing the message from the function also
代码4:从功能输入年龄并从功能打印消息
<html lang="en">
<head>
<script>
function validate(){
var age=parseInt(prompt("Enter age"));
var ans="not eigible";
if(age>=18){
ans="eligible";
}
document.write("You are <b>"+ans+"</b> for Vote");
}
</script>
</head>
<body>
<script>
validate();
</script>
</body>
</html>
Output
输出量