Why this is not working

Hello everyone for the following lesson

for the Active learning: Filling in a guest list

why the replacing miss behave here

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


// removing the extra space
refused.textContent = refused.textContent.trimEnd();


//replacing the comma with .
refused.textContent = refused.textContent.replace(refused.textContent[refused.textContent.length-1],’.’);



admitted.textContent = admitted.textContent.trimEnd();


admitted.textContent = admitted.textContent.replace(admitted.textContent[admitted.textContent.length-1],’.’);


it replace the comma after the first name not the last one

can anyone let me know what i am missing here

i know there a solution to it but i want to discover what i a missing

thanks and have a nice day

I think the problem is in your usage of replace:

According to

Mdn page about replace

If pattern is a string, only the first occurrence will be replaced.

Either use replaceAll or specify an appropriate Regex

thanks @aviv.mu

but refused.textContent[refused.textContent.length-1] should return the character in the string not the first accurance

i added the following

it return the last comma and to double check it
i used console.log(refused.textContent[refused.textContent.length-2]);
and it returened a the last letter or lola

and even change it to -2 and -3 and work fine but the replace still did not work

i think there are something else going on

thanks alot for your time and help and have a nice day

Did you try replaceAll?

@aviv.mu it replace all comma with .

hope @chrisdavidmills help us here

i got it the refused.textContent[refused.textContent.length-1] return the value which is comma then the replace as you @aviv.mu said replace the first accurance

thanks @aviv.mu

and thanks @chrisdavidmills no problem i got it now

have a nice day both of you

@justsomeone @aviv.mu I was just getting ready to jump in with some help, but then I see you got it worked out. Well done!

thanks @aviv.mu @chrisdavidmills