Home > Web Front-end > JS Tutorial > body text

Application examples of js string

小云云
Release: 2018-03-26 15:16:14
Original
1582 people have browsed it

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;
    }
Copy after login

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!

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