JavaScript | 输入年龄和检查人员是否可以投票的代码

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

输出量

Js code output
Js code output

翻译自: https://www.includehelp.com/code-snippets/input-age-and-check-person-is-eligible-for-voting-or-not.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值