Viết một chương trình JavaScript để tính điểm trung bình của các học sinh sau đây Sau đó, điểm trung bình này được sử dụng để xác định loại.
| Sinh viên | Điểm |
|---|---|
| David | 80 |
| Vinoth | 77 |
| Divya | 88 |
| Ishitha | 95 |
| Thomas | 68 |
Xác định loại như sau:
| Điều kiện | Loại |
|---|---|
| <60 | F |
| <70 | D |
| <80 | C |
| <90 | B |
| <100 | A |
Ví dụ:

Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Compute the average marks and grade</title>
<script>
var students = [['David', 80], ['Vinoth', 77], ['Divya', 88], ['Ishitha', 95], ['Thomas', 68]];
var Avgmarks = 0;
for (var i=0; i < students.length; i++) {
Avgmarks += students[i][1];
var avg = (Avgmarks/students.length);
}
console.log("Average grade: " + (Avgmarks)/students.length);
if (avg < 60){
document.write("Grade : F");
}
else if (avg < 70) {
document.write("Grade : D");
}
else if (avg < 80)
{
document.write("Grade : C");
} else if (avg < 90) {
document.write("Grade : B");
} else if (avg < 100) {
document.write("Grade : A");
}
</script>
</head>
<body>
</body>
</html>
Lưu đồ thuật toán:

Thấy bài viết hữu ích?
Ủng hộ tác giả để có thêm nhiều bài viết chất lượng nhé!