-
Notifications
You must be signed in to change notification settings - Fork 3
/
29.html
44 lines (37 loc) · 1.09 KB
/
29.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<script>
/**
* Đề bài: Nhập vào một số nguyên k, Tính và in ra trung bình cộng của các số nguyên tố nhỏ hơn hoặc bằng k. Kết quả làm tròn tới 3 chữ số thập phân.
*/
function main() {
const k = Number(prompt("Nhập dãy số, ngăn cách nhau bằng dấu cách, kết thúc bằng số 0", 1))
let numbers = ''
for (let i = k; i > 1; i--) {
if (isNguyenTo(i)) {
numbers += String(i)
}
}
let sum = 0
let count = 0
for (let i = 0; i < numbers.length; i++) {
sum += Number(numbers.charAt(i))
count++
}
// Math.round() là hàm làm tròn số
// Chi tiết xem tại: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
const average = Math.round(sum/count * 100) / 100
console.log({ average })
}
function isNguyenTo(n) {
if (n <= 1) {
return false
}
let isNguyenTo = true
for (let i = 2; i < n; i++) {
if (n % i == 0) {
return false
}
}
return isNguyenTo
}
main()
</script>