Hi can anyone help I am currently stuck on this exercise where I need to add tutors from a Javascript object to an empty array. The description is as follows:
The Big Three Oh
For this first challenge, complete the code that iterates through tutorAges and add the names of all tutors aged 30 to the tutorsAgedThirty array.
This is what I have done so far after trying many different combinations and still stuck. I get the message " ✕Not quite! It would be good to name the key variable tutor here
I am including below the code that I have done so far, any help would be appreciated.
const tutorAges = {
"Lee": 30,
"Rich": 26,
"Christian": 30,
"Jodie": 25,
"Roisin": 24
};
const tutorsAgedThirty = []
// Start typing below this line
for (const key in tutorAges){
const checkTutor = tutorAges[key]
if (tutorAges[key]>=30){
tutorsAgedThirty.push(checkTutor)
console.log(key + tutorsAgedThirty)
}
}
const tutors = []; // Initialize an empty array
const tutorObject = { name: 'John', subject: 'Math' }; // Example tutor object
tutors.push(tutorObject); // Add the tutor to the array
Replace { name: 'John', subject: 'Math' } with your actual tutor object.
Adding multiple tutors to the array:
If you have multiple tutor objects, you can loop through them and add each one to the array. For instance:
const tutors = []; // Initialize an empty array
const tutor1 = { name: 'John', subject: 'Math' };
const tutor2 = { name: 'Alice', subject: 'Science' };
// Add tutors to the array
tutors.push(tutor1);
tutors.push(tutor2);
// Repeat for other tutors if needed
Adjust the tutor objects (tutor1, tutor2, etc.) according to your data.
Using the spread operator (ES6):
You can also use the spread operator to add multiple tutors at once:
JavaScript
const tutors = []; // Initialize an empty array
const tutor1 = { name: 'John', subject: 'Math' };
const tutor2 = { name: 'Alice', subject: 'Science' };
// Add tutors to the array using spread
tutors.push(...[tutor1, tutor2]);
// Repeat for other tutors if needed
This approach is concise and works well when you have an array of tutor objects.
Remember to replace the example tutor objects with your actual data. Once you’ve added the tutors, the tutors array will contain all the tutor objects.
Hi thank you very much for your answer I actually solved it and will include the code below just in case it helps someone else in a pickle. The only thing is that I don’t know how to close the topic.
const tutorAges = {
"Lee": 30,
"Rich": 26,
"Christian": 30,
"Jodie": 25,
"Roisin": 24
};
const tutorsAgedThirty = []
// Start typing below this line
for (const key in tutorAges){
const moveKey = ""
if (tutorAges[key] === 30){
tutorsAgedThirty.push(moveKey + key)
}
}