Three-digit daffodil number, parseInt, push in javascript

Article Directory


the code

function daffodilNumber() {
    
    
	let count = 0,
		arr = [];
	
	for (let i = 100; i < 999; i++) {
    
    
		let ge = i % 10,
			shi = parseInt(i / 10) % 10,
			bai = parseInt(i / 100),
			isDN = (ge * ge * ge + shi * shi * shi + bai * bai * bai) === i;
		
		if ((ge * ge * ge + shi * shi * shi + bai * bai * bai) === i) {
    
    
			count++;
			
			arr.push({
    
    
				id: 'id' + i,
				value: i
			});
		}
	}
	
	return {
    
     count, arr };
}

let {
    
     count, arr } = daffodilNumber();

console.log('水仙花的个数为: ', count);
// 水仙花的个数为:  4

arr.forEach(item => console.log('水仙花的数值为: ', item.value));
// 水仙花的数值为: 153
// 水仙花的数值为: 370
// 水仙花的数值为: 371
// 水仙花的数值为: 407

parseInt

MDN

parseInt(string, radix)Parses a string and returns a decimal integer in the specified radix, radixwhich is 2-36an integer between, representing the radix of the string being parsed.
string
The value to be parsed. If the argument is not a string, it is converted to a string (using the ToString abstract operation). Whitespace at the beginning of the string will be ignored.
radixOptional Integer
from 2to 36, representing the base of the base. For example, 16specifying that the parsed value is a hexadecimal number. Will return if outside of this range NaN. If specified 0or not specified, the radix will be inferred from the value of the string. =The extrapolated result will not always be the default 10! The description at the end of the article explains radixthe specific behavior of the function when the parameter is not passed.

w3school

parseInt()The function parses the string and returns an integer.
radixThe parameter is used to specify which number system to use, e.g. radix is 16​​(hexadecimal) to indicate that the numbers in the string should be parsed from hexadecimal to decimal.
If radixthe argument is omitted, JavaScriptthe following is assumed:
if the string begins "0x"with , the radix is 16​​(hexadecimal)
If the string "0"starts with , the radix is 8​​(octal). This attribute is deprecated.
If the string starts with any other value, the base is 10(decimal)
Only returns the first number in the string!
Leading and trailing spaces are allowed.
parseInt()Returns if the first character cannot be converted to a number NaN.
== Older browsers will result parseInt("010")in , because 8older versions use an octal radix as the default when the string starts with . From start, the default is base-decimal .ECMAScriptECMAScript5"0"8ECMAScript510


push

MDN

pushmethod adds the specified element to the end of the array and returns the new array length.


w3school

pushmethod adds a new item to the end of the array and returns the new length.
New items will be added to the end of the array.
pushmethod changes the length of the array.
To add items at the beginning of the array, use unshiftthe method.

Guess you like

Origin blog.csdn.net/weixin_51157081/article/details/131757398