Pages

Entrainement ! 🚀

const students = [
  { name: "Alice", marks: [10, 14, 17] },
  { name: "Mohamed", marks: [15, "ABJ", 15] },
  { name: "Bob", marks: [] }, // No marks
  { name: "Charlie" }, // No marks property
  { name: "David", marks: ["ABI", "ABI", 20] }]

🚀Donnez le résultat lors de l'application du MCC

[
    { name: 'Alice', result: 'ADM', averageMarks: '13.67' },
    { name: 'Mohamed', result: 'ADM', averageMarks: '15.00' },
    { name: 'Bob', result: 'AJ' },
    { name: 'Charlie', result: 'AJ' },
    { name: 'David', result: 'AJ' }
]

function calculateAverageMarks(students) {
    return students.map((student) => {
      if (!student.marks
|| student.marks.length === 0
|| student.marks.includes("ABI")) {
        return { name: student.name, result: "AJ" };
      }
 
      const validMarks = student.marks.filter(mark => typeof mark === 'number');
      if (validMarks.length === 0) {
        return { name: student.name, result: "AJ" };
      }
 
      const totalMarks = validMarks.reduce((sum, mark) => sum + mark, 0);
      const averageMarks = (totalMarks / validMarks.length).toFixed(2);
      return { name: student.name, result: "ADM", averageMarks };
    });
  }
 

  console.log(calculateAverageMarks(students));