Home > Web Front-end > JS Tutorial > How to use JavaScript loop practice to find the palindrome number between 1~9999

How to use JavaScript loop practice to find the palindrome number between 1~9999

一个新手
Release: 2017-10-13 09:18:05
Original
3518 people have browsed it

Find an integer between 1 and 9999, determine whether it is a palindrome number, if so, output and output the number of digits in this number


<script type="text/javascript">/*先整理思路、、、、、    
var n = 3223;   只要前后颠倒相等就是回文数
整数的位数: n.length
for(var i = n.length - 1; i <= o ; i--){
    m.push(n[i]);
}
    document.write(m);    
    */
    function huiwen(){    var num = prompt("请输入一个整数");
    document.write("你输入的数是:" + num + "<br>");    
    var n ="";  //定义颠倒后的字符串 
    if(num<1){
       document.write("不要输入负数");
       }else if (num >= 1 &&num <10){
            document.write("这是一个回文数" + "<br>");
            document.write("这个回文数的位数是:1");
       }        else{          for(var i = num.length ; i >= 0  ; i--){ 
              var num1 = num.charAt(i);     //charAt等同于数组的下标,这样通过for循环就可以把输入的整数顺序颠倒
              n = n.concat(num1);            // 但是num1并不是一个整数,也不是一个字符串,没法跟num比较,用concat()将它们连接成一个字符串            }
             document.write( n + ":");      //得到的回文数输出一下             
             if( n == num){
               document.write("这是一个回文数" + "<br>");
               document.write("这个回文数的位数是:" + n.length)
            }else{
               document.write(&#39;这不是一个回文数&#39;);
            }
       }
}</script>
</head>

<body>
<input type="button" value="开始" onClick="huiwen()">
</body>
Copy after login

The above is the detailed content of How to use JavaScript loop practice to find the palindrome number between 1~9999. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template