Active learning: Filling in a guest list

This is the code I came up with for the Active learning: filling in a guest list. The live output was not working properly and I saw that my code was different from the solution. I wanted to know if my code accomplishes the same as the solution given. Please give me feedback on what I could do differently. Here is the link to the active learning: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Looping_code

const people = [‘Chris’, ‘Anne’, ‘Colin’, ‘Terri’, ‘Phil’, ‘Lola’, ‘Sam’, ‘Kay’, ‘Bruce’];

const admitted = document.querySelector(’.admitted’);
const refused = document.querySelector(’.refused’);
admitted.textContent = 'Admit: ';
refused.textContent = 'Refuse: ’

let i = 0;

while (i < people.length) {
if(people[i] === ‘Phil’ || people[i] === ‘Lola’) {
refused.textContent += people[i] + ', ';
} else {
admitted.textContent += people[i] + ', ';
}
i++;
}

refused.textContent = refused.textContent.replace(’, ‘, ‘.’);
admitted.textContent = admitted.textContent.replace(’, ', ‘.’);

Hi @Arthur_Chen.

Your code looks to be working fine, except that the very last two lines are causing the first instance of , to be replaced with ., whereas you should be replacing the last instance. This is because when you use replace() with a string, it only finds and replaces the first instance it finds.

My solution to replace the last comma with a full stop is as follows:

refused.textContent = refused.textContent.slice(0,refused.textContent.length-2) + '.';
admitted.textContent = admitted.textContent.slice(0,admitted.textContent.length-2) + '.';

So basically I’m slicing each string to remove the last two characters off the end of it, then add a full stop back onto the end.

Thanks for the feedback! I’m curious if .replace(refused.TextContent.length-2, ‘.’) would also work.

@Arthur_Chen try it and see what you find out :wink: