'function to check if string is the same reading from back removing commas and spaces, javascript

this function check if a string is the same if you read it from back to start and it returns true if it does and false if not. The function is working but I can not use it in a test because it contains "replaceAll" , so I need to modify using some sort of basic javascript without regexp or "replaceALL" / "split" / "join" etc. The function has to remove commas and empty spaces from the string also to lower its chars.

function reversible(target) {
  let lower = target.toLowerCase();
   let comma = lower.replaceAll(',','');
  let empty = comma.replaceAll(' ','');
  let result = '';
  for ( let i = empty.length - 1 ; i >= 0 ; i--){
    result += empty[i];
    } 
  if (result === empty) {return true;}
  else{return false;}
}
console.log(reversible('rotator'));
console.log(reversible('home'));
console.log(reversible('Racecar'));
console.log(reversible('eva, can i see bees in a cave'))


Solution 1:[1]

var reversible = function(string) {
  var trimmedString = string.replace(/[, ]+/g, "").trim();
  var reverseString = trimmedString.split('').reverse().join('');
  return trimmedString == reverseString;
}

console.log(reversible('rotator'));
console.log(reversible('home'));
console.log(reversible('Racecar'));
console.log(reversible('eva, can i see bees in a cave'))

Solution 2:[2]

This loops through half the string, ignoring spaces and commas, and checking that the letter at the beginning matches the letter at the end, and so on.

function reversible(target) {
  target = target.toLowerCase();
  const l = target.length;
  let i = 0;
  let j = l - 1;
  let output = "";
  let output2 = "";
  
  do {
    while (target[i] === " " || target[i] === ",") {
      i++;
    }
    while (target[j] === " " || target[j] === ",") {
      j--;
    }
    if (i < j) {
      if (target[i] === target[j]) {
        output += target[i];
        output2 = target[i] + output2;
        i++;
        j--;
      } else {
        return "Not reversible";
      }
    }
  } while (i < j);
  
  if (i === j) {
    // odd number of letters
    return output + target[i] + output2;
  } else {
    return output + output2;
  }
}

console.log(reversible('rotator'));
console.log(reversible('home'));
console.log(reversible('Racecar'));
console.log(reversible('eva, can i see bees in a cave'))

Solution 3:[3]

you can create a new string and use the method concat to add the string without commas or spaces

  function reversible(target) {
  let lower = target.toLowerCase();
  let empty=""
  for ( let i = 0 ; i <=lower.length-1 ; i++){
    //if character different of "," or space
    if (lower[i]!==" " & lower[i] !== ","){
    //concatenate character
      empty=empty.concat(lower[i])
    }

    } 
  result=""
  for ( let i = empty.length - 1 ; i >= 0 ; i--){
    result += empty[i];
    } 
  return result==empty[i]
}


console.log(reversible('rotator'));
console.log(reversible('home'));
console.log(reversible('Racecar'));
console.log(reversible('eva, can i see bees in a cave'))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tushar Wason
Solution 2 James
Solution 3