2
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đang học JavaScript trên Freecodecamp. Và, giải pháp cho vấn đề của tôi không giúp tôi nhiều hơn. Chúng ta phải lọc các số nguyên trong một mảng được lưu trong biến const.
const realNumberArray = [4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2];
const squareList = [arr] => {
"use strict";
const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
};
const squaredIntegers = squareList[realNumberArray];
console.log[squaredIntegers];
Và tôi không thể hiểu thực sự phần này num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
Tại sao sử dụng phương thức parseInt [] và toán học. Ai đó có thể giải thích cho tôi tại sao?
thanks.
Đã hỏi ngày 14 tháng 1 năm 2019 lúc 11:43Jan 14, 2019 at 11:43
2
parseInt[num]
cho một phần số nguyên của num, ví dụ, parseInt[3.14] === 3 //true
. Sử dụng
const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
0 về cơ bản mang lại sự khác biệt giữa số và phần nguyên của nó. Nếu nó không phải là const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
1, số lượng sẽ bị loại bỏ. const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
2 đưa ra một số bình phương, được trả lại cho mảng mới. Mặc dù, const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
3 nhanh hơn về vấn đề đó, không phải bao gồm một mô -đun và gọi một thuộc tính đối tượng.Ngoài ra, mã rất chật chội trong giải pháp và tôi sẽ đề nghị phá vỡ nó để cải thiện khả năng đọc. Có vẻ như phong cách mà nó được viết thêm vào sự nhầm lẫn.
Đã trả lời ngày 14 tháng 1 năm 2019 lúc 11:50Jan 14, 2019 at 11:50
const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
Ở đây bên trong
const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
4, nó được kiểm tra nếu const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
5 dương tính const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
6 và const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
5 là một const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
8. Để kiểm tra số nguyên. const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
0 ParseInt thay đổi num thành const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
return squaredIntegers;
8 và const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
1 của một số có chính nó là 0 nên điều kiện const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
2. const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
3 được sử dụng để vuông num.Đã trả lời ngày 14 tháng 1 năm 2019 lúc 11:52Jan 14, 2019 at 11:52
Ellipsisellipsisellipsis
11.8k2 Huy hiệu vàng15 Huy hiệu bạc33 Huy hiệu đồng2 gold badges15 silver badges33 bronze badges
const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
Đây có thể là một giải pháp dễ dàng hơn cho vấn đề của bạn, mặc dù tất cả các giải pháp trên cũng hoạt động tốt.
const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
4 cũng có thể được thay thế bằng const squaredIntegers = arr.filter[
[num] => {
if [Number.isInteger[num] && num >0]{
return num;
}}
].map[[num] => num *num]; // Math.pow[num, 2] can also be used here
return squaredIntegers;
};
5.
Laurenz mặc dù
Phim thương hiệu vàng 180K1616 gold badges158 silver badges206 bronze badges
Đã trả lời ngày 8 tháng 8 năm 2019 lúc 6:19Aug 8, 2019 at 6:19
Hãy thử cái này, nó sẽ không chỉ lọc ra âm một khi chỉ trả về những số nguyên.
const squareList = arr => {
let myArr = arr
.filter[item => item > 0]
.reduce[[a, current] => a.concat[current*current] , []]
.filter[num => Number.isInteger[num]];
return myArr;
};
const squaredIntegers = squareList[[4, 5.6, -9.8, 3.14, 42, 6, 8.34, -2]];
console.log[squaredIntegers];// Prints : [16, 1764, 36] on console.
Đã trả lời ngày 9 tháng 7 năm 2021 lúc 22:08Jul 9, 2021 at 22:08
Nhiều người đã giải thích điều này theo một cách rất tốt. Bạn cũng có thể làm phần này
const squaredIntegers = arr.filter[ [num] => num > 0 && num % parseInt[num] === 0 ].map[ [num] => Math.pow[num, 2] ];
Như thế này, và nó nên hoạt động mà không có vấn đề. Hy vọng, điều này sẽ giúp.
const squaredIntegers = arr.filter[ [num] => num > 0 && num % 1 === 0 ].map[ [num] => num * num ];
Đã trả lời ngày 15 tháng 1 năm 2019 lúc 8:36Jan 15, 2019 at 8:36
Một cách đơn giản hơn để sử dụng câu lệnh giảm là điều này
const squareList = arr => {
// Only change code below this line
let myArr = arr.filter[user => Number.isInteger[user] && user > 0]
.reduce[[sqr,num] => sqr.concat[num*num], []];
return myArr;
// Only change code above this line
};
const squaredIntegers = squareList[[-3, 4.8, 5, 3, -3.2]];
console.log[squaredIntegers];
trong đó number.isinteger [] được sử dụng để lọc số thập phân và người dùng> 0 được sử dụng để lọc các giá trị âm từ mảng
Đã trả lời ngày 28 tháng 5 lúc 11:49May 28 at 11:49