I'm using splice to remove the last n
number of elements on an array (denoted by quizResponseCountToRemove
).
However, in its currently implementation, it only removes 1 element.
Is there any particular reason for this?
My code is as follows;
static removeCourseContentElementQuizResponseCount(
course: ICourse,
courseContents: ICourseContent[],
selectedCourseContentUid: string,
selectedCourseModule: number,
quizResponseCountToRemove: number
): ICourse {
courseContents.map((courseContent) => {
if (courseContent.uid === selectedCourseContentUid) {
courseContent.quiz.options.splice(-1, quizResponseCountToRemove);
if (courseContent.quiz.answer > courseContent.quiz.options.length) {
courseContent.quiz.answer = 1;
}
}
});
course.modules[selectedCourseModule].content = courseContents;
return course;
}
Update
It seems I have misunderstood how splice works. I want splice to work backwards from the last element. So in an array ['test1', 'test2', 'test3', 'test4']
, if quizResponseCountToRemove
is 2 then I end up with ['test1', 'test2']
.