Home > front end >  Error in my task 4, it seems that .checked and .value doesn't seem to work as intended
Error in my task 4, it seems that .checked and .value doesn't seem to work as intended

Time:11-20

Link to Scrimba to check on code : https://scrimba.com/scrim/czvrQJcM

Essentially what should happen is that when the details are added and the button is clicked, the data first gets saved into the object, 'student' and pushed into the array. Then the array is run and the students names are displayed on the page.

CodePudding user response:

I just solve your problem. Look: At your enrolStudent() function you set the following line:

    studentList = studentList.push(student);

So, you should change it to:

    studentList.push(student);

This is because the push() method returns the length of the array. So, you don't need to attribute the operation result to the array. The method already updated its own value.

Task4 after the change:

    function enrolStudent()
    {
        let firstnameRef = document.getElementById("fname");
        let lastnameRef = document.getElementById("lname");
        let studentIdRef = document.getElementById("studentid");
        let dateRef = document.getElementById("date");
        let courseRef = document.getElementById("course");
        let genderRef = document.getElementsByName("gender");
        for (let i=0; i < genderRef.length; i  )
        {
            if (genderRef[i].checked)
            {
                var genderSelection = genderRef[i];
            }
        }
        let student = new Object();
        student.firstName = firstnameRef.value;
        student.lastName = lastnameRef.value;
        student.id = studentIdRef.value;
        student.course = studentIdRef.value;
        student.gender = genderSelection.value;
        student.date = dateRef.value;
        studentList.push(student);
        var val = studentList.push(student);
        studentEnrolSummaryRef.innerHTML = displayStudents(studentList);
        console.log(val);
    }

```
  • Related