Home > Article > Web Front-end > Application examples of js string
This article mainly shares with you application examples of js strings, hoping to help everyone.
1. Symmetrical numbers
Symmetrical numbers are the same as the original value when a number is recalled. For example: 11, 22, 111, etc.
Now use the learned js knowledge to write a function that takes all symmetric numbers within a certain range.
function reverseToNum(num){ //将该数值反转,取其反转后的值 'use strice'; var num = num; var str = num.toString().split(""); var reverseStr = str.reverse().join(""); var reverseNum = Number(reverseStr); return reverseNum; } function isReverse(num){//判断该数值是否为对称数 'use strice'; var num = num; if(num == reverseToNum(num)){ return num; }else{ return false; } } function countReverse(num){//计算某范围内对称数的总个数,将对称数存入数组并返回 'use strice'; var num = num; var reverseArr = []; if(typeof(num)!='number'){ alert("please enter a number"); return false; }else if(num<=0){ alert("please enter positive integer"); return false; } for(var i=1;i<=num;++i){ if(isReverse(i)){ reverseArr.push(i); } } return reverseArr; }
Related recommendations:
JS string removal method of consecutive and repeated characters
The above is the detailed content of Application examples of js string. For more information, please follow other related articles on the PHP Chinese website!