Як за допомогою JavaScript можна видалити останню кому, але лише якщо кома є останнім символом або якщо після коми є лише пробіл? Це мій код. Я отримав робочу скрипку . Але в ньому є помилка.
var str = 'This, is a test.';
alert( removeLastComma(str) ); // should remain unchanged
var str = 'This, is a test,';
alert( removeLastComma(str) ); // should remove the last comma
var str = 'This is a test, ';
alert( removeLastComma(str) ); // should remove the last comma
function removeLastComma(strng){
var n=strng.lastIndexOf(",");
var a=strng.substring(0,n)
return a;
}