码迷,mamicode.com
首页 > 其他好文 > 详细

有一个八位数,个位数+十位数+百位数+千位数一直加到千万位数的和除以7能等于0.。。。

时间:2017-10-04 00:22:14      阅读:366      评论:0      收藏:0      [点我收藏+]

标签:int()   功能   dom   sel   for   之间   string   value   random   

某天晚上,一个在学java同学突然提出一个问题,有一个八位数,把它每一位数的数字相加的和如果能除以7等于0,那么就算中奖了!于是我就用javascript来实现了这个功能....如下:

<script>
window.onload = () => {
    var aInput = document.querySelectorAll(‘input‘);
    var oSpan = document.querySelectorAll(‘span‘);
    aInput[8].onclick = function(){
        aInput[0].value = Math.ceil(Math.random()*9);//求1-9之间的随机数, Math.ceil()向上取整;
        for(let i = 1 ; i < aInput.length-1 ; i++ ){
            aInput[i].value = Math.floor(Math.random()*10);//0-9之间的随机数, Math.floor()向下取整;
        }
        oSpan[0].innerHTML = aInput[0].value + aInput[1].value + aInput[2].value + aInput[3].value + aInput[4].value + aInput[5].value + aInput[6].value + aInput[7].value;//aInput[0].value 是string类型,后面要用parseInt()转成int整形;
        if( (parseInt( aInput[0].value ) + parseInt( aInput[1].value ) + parseInt( aInput[2].value ) + parseInt( aInput[3].value ) + parseInt( aInput[4].value ) + parseInt( aInput[5].value ) + parseInt( aInput[6].value ) + parseInt( aInput[7].value ) ) % 7 == 0 ){ //从个位数数字加到千万位数数字的和除以7能整除;
            alert(‘您的中奖号码是:‘ + aInput[0].value + aInput[1].value + aInput[2].value + aInput[3].value + aInput[4].value + aInput[5].value + aInput[6].value + aInput[7].value);//得奖号码
        }
    }
}
</script>

style格式如下:

<style>
    input{
        width: 15px;
    }
    #btn{
        width: 50px;
    }
</style>
<body>
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input id="btn" type="button" value="刷新">
<span></span>
</body>

 

有一个八位数,个位数+十位数+百位数+千位数一直加到千万位数的和除以7能等于0.。。。

标签:int()   功能   dom   sel   for   之间   string   value   random   

原文地址:http://www.cnblogs.com/ZGMF/p/7624824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!