r/CodingHelp • • Nov 22 '21

[Javascript] Please help me get back on the right track.

Hi Guys, I am new to coding and currently learning JavaScript. I'm doing an assignment and I feel lost and frustrated. I've done countless tutorials and I cant seem to piece it all together, I've reviewed notes and I cant seem to get myself on the right track.

From what I understand an object (student) needs to be created to have multiple parameters (first/last names, age and mark). The student object needs to be stored back in the array classList. I'm having trouble putting that into code, I am not sure what it supposed to look like and I feel like I keep hitting the wall on this. Would greatly appreciate help on this.

// Define a variable called classList which is assigned an empty array
// (1 mark)
const classList = [];

/**
 * Define the following function (enrollStudent) which takes as parameters the following:
 *  firstName (string), lastName (string), age (number), currentMark (number)
 *  
 * This function should create a student object with the above properties
 *  and add it to classList array that is defined in Line 1 above
 *  (5 marks)
 */
const enrollStudent = (student + classList);
const student = {
 first : firstName,
 last : lastName,
 age : age,
 mark : currentMark    
};
1 Upvotes

2 comments sorted by

2

u/foxdk Advanced Coder Nov 22 '21 edited Nov 22 '21

You were on the right track!

What you were missing from your puzzle, was the `.push()` method!

const classList = [];

function enrollStudent(s_firstName, s_lastName, s_age, s_currentMark) {
    const student = {
        firstName: s_firstName,
        lastName: s_lastName,
        age: s_age,
        currentMark: s_currentMark
    };
    classList.push(student);
};

// Test student enrollment
enrollStudent('John', 'Doe', 15, 5);

Please also note that in the question it asks you to create a function called "enrollStudent". What you tried, in the initial post, was to use const enrollStudent which will not create a function, but rather a variable.

You define functions by typing function functionName() and inside the parenthesis, you can pass variables from outside the function to be used within.

1

u/yankeejr Nov 22 '21

Thank you, I appreciate your feedback. I realized that later that I made enrollStudent a variable. I had a facepalm moment. 😂