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,radix
which is2-36
an 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.
radix
Optional Integer
from2
to36
, representing the base of the base. For example,16
specifying that the parsed value is a hexadecimal number. Will return if outside of this rangeNaN
. If specified0
or not specified, the radix will be inferred from the value of the string. =The extrapolated result will not always be the default10
! The description at the end of the article explainsradix
the specific behavior of the function when the parameter is not passed.
w3school
parseInt()
The function parses the string and returns an integer.
radix
The parameter is used to specify which number system to use, e.g. radix is16
(hexadecimal) to indicate that the numbers in the string should be parsed from hexadecimal to decimal.
Ifradix
the argument is omitted,JavaScript
the following is assumed:
if the string begins"0x"
with , the radix is16
(hexadecimal)
If the string"0"
starts with , the radix is8
(octal). This attribute is deprecated.
If the string starts with any other value, the base is10
(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 numberNaN
.
== Older browsers will resultparseInt("010")
in , because8
older versions use an octal radix as the default when the string starts with . From start, the default is base-decimal .ECMAScript
ECMAScript5
"0"
8
ECMAScript5
10
push
MDN
push
method adds the specified element to the end of the array and returns the new array length.
w3school
push
method 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.
push
method changes the length of the array.
To add items at the beginning of the array, useunshift
the method.