I have this code to remove the first and last spaces but it is removing all the spaces present. Following:
const separador = ' ';
function filtro(separador, str) {
let resultado = '';
let stringNova = '';
for (let i = 0; i < str.length; i++) {
if (str[i] === separador) {
resultado += str[i];
} else {
stringNova += str[i];
}
}
return stringNova;
}
console.log(filtro(separador, (" X DSA ")));
Should return:
X DSA
and not XDSA
.
Could someone help me?
Personal, in real I am creating a Trim method myself, so I do not want to use the Trim method of JS itself