How to remove an item from an array of objects for a function?

86 Views Asked by At

1.Given an array of submission objects, when I supply a date, an array of student names, and an array of submission objects to the findUnsubmitted function, I am returned an array of names of students that haven't completed any quiz on that date.

  1. Given the the findUnsubmitted feature doesn't find any student names, I am returned an empty array.

When I console.log my findUnsubmitted function, I am just returned: [ 'Kevin', 'Nivek', 'John' ] ... when in this case, I should only be returned Kevin and John in the array...

const submissions = [
    {
        quizName: "Quiz 1",
        quizModule: "Math",
        quizScore: 100,
        studentId: 001,
        studentName: "Kevin",
        submissionDate: "March 24, 2022"
    },
    {
        quizName: "Essay",
        quizModule: "English",
        quizScore: 0,
        studentId: 023,
        studentName: "Nivek",
        submissionDate: "April 1, 2022"
    },
    {
        quizName: "Quiz 2",
        quizModule: "Science",
        quizScore: 71.59485,
        studentId:023,
        studentName: "John",
        submissionDate: "May 24, 2022"
    }

]

const filterByDate = (particularDate, submission) => {
    return submission.filter((sub) => sub.submissionDate === particularDate)
}

const findUnsubmitted = (particularDate, names, submission) => {
    const date = filterByDate(particularDate, submission);
    const unsubmitted = names;
    for(let i = names.length - 1; i >=0; i--) {
        const student = date[i];
        if(student == names.studentName){
            submissions.splice(i, 1);
        }
        return unsubmitted;
        }
}

console.log(findUnsubmitted('April 1, 2022', ['Kevin', 'Nivek', 'John'], submissions));
2

There are 2 best solutions below

0
Hungnn On

If you just want to return an array of names of students that haven't completed any quizzes on that date. Don't make it complicated

In function: findUnsubmitted don't need using names variable.


Change code in function filterByDate like:

const filterByDate = (particularDate, submission) => {
     return submission.filter((sub) => sub.submissionDate !== particularDate)
}

and function findUnsubmitted:

const findUnsubmitted = (particularDate, submission) => {
      const date = filterByDate(particularDate, submission);
      if (!date) return [];
      return date.map(item => item.studentName);
}

Hope that it helps you.

0
vijay pancholi On

The issue in your code is condition if(student == names.studentName) is incorrect. names is an array, and you should compare with names[i] instead of names.studentName. Also, you should use === for strict equality.

You are returning unsubmitted inside the loop, which means the loop will terminate after the first iteration, and return resubmitted to outside the loop

const submissions = [
        {
            quizName: "Quiz 1",
            quizModule: "Math",
            quizScore: 100,
            studentId: 001,
            studentName: "Kevin",
            submissionDate: "March 24, 2022"
        },
        {
            quizName: "Essay",
            quizModule: "English",
            quizScore: 0,
            studentId: 023,
            studentName: "Nivek",
            submissionDate: "April 1, 2022"
        },
        {
            quizName: "Quiz 2",
            quizModule: "Science",
            quizScore: 71.59485,
            studentId: 023,
            studentName: "John",
            submissionDate: "May 24, 2022"
        }
    ];
    
    const filterByDate = (particularDate, submission) => {
        return submission.filter((sub) => sub.submissionDate === particularDate);
    }
    
    const findUnsubmitted = (particularDate, names, submission) => {
        const date = filterByDate(particularDate, submission);
        const unsubmitted = [];
    
        for (let i = 0; i < names.length; i++) {
            const studentName = names[i];
    
            // Check if the student has a submission on the given date
            const submitted = date.some(sub => sub.studentName === studentName);
    
            // If not submitted, add to unsubmitted array
            if (!submitted) {
                unsubmitted.push(studentName);
            }
        }
    
        return unsubmitted;
    }
    
    console.log(findUnsubmitted('April 1, 2022', ['Kevin', 'Nivek', 'John'], submissions));