1

I've been using a function to turn different strings into snake_case. When I try to use Array.toString() to convert the final array into a string, the function still returns an array. For example, try identify(punctuated). Why is underlinedStringNew an array instead of a string?

Best regards Beni

let punctuated = ',Hello, World???'

function identify(input) {
  let array = input.split("");
  let smallArray = [];
  let newestArray = [];

  for (let i = 0; i < array.length; i++) {
    smallArray[i] = array[i].toLowerCase();
  }

  for (let i = 0; i < array.length; i++) {
    if (array[i] == "." && array[i + 1] == ".") {
      return "WtfCase";
    }
  }

  let underlinedString = input.replace(/[^A-z]/g, "_");

  let smallUnderlinedArray = [];
  let capitalizedLetters = [];
  let underlinedArray = underlinedString.split("");
  console.log("Underlined Array: " + underlinedArray);

  for (let i = 0; i < underlinedArray.length; i++) {
    smallUnderlinedArray[i] = underlinedArray[i].toLowerCase();
  }

  for (let i = 1; i < underlinedArray.length; i++) {
    if (underlinedArray[i] !== smallUnderlinedArray[i]) {
      capitalizedLetters.push(i);
    };
  };

  for (let i = 0; i < capitalizedLetters.length; i++) {
    underlinedArray.splice(capitalizedLetters[i], 0, "_");
  }

  let i = 0;
  for (i; i < underlinedArray.length; i++) {
    if (underlinedArray[i] == "_" && underlinedArray[i + 1] == "_") {
      underlinedArray.splice(i, 1);
      i = i - 1;
    };
  };

  if (underlinedArray[underlinedArray.length - 1] == "_") {
    underlinedArray.splice(underlinedArray.length - 1, 1);
  }

  if (underlinedArray[0] == "_") {
    underlinedArray.splice(0, 1);
  }

  let underlinedStringNew = underlinedArray.toString();
  console.log("Should be a string: " + underlinedStringNew);

};
Huangism
  • 16,278
  • 7
  • 48
  • 74
Benisburgers
  • 352
  • 4
  • 17

2 Answers2

3

I think that what you are looking is to join elements of you array without ,. By default, join add ,. If you want to remove them use an empty string ''.

// note that I simply a bit your code just to showcase the solution.
var a = ',Hello, World???'.replace(/[^A-z]/g, "_").split("").join('');
console.log("Should be a string: " + a);

Output:

Should be a string: _Hello__World___

If you want to learn more about join, check the MDN. Some examples:

var elements = ['Fire', 'Wind', 'Rain'];

console.log(elements.join());
// expected output: Fire,Wind,Rain

console.log(elements.join(''));
// expected output: FireWindRain

console.log(elements.join('-'));
// expected output: Fire-Wind-Rain
aloisdg
  • 22,270
  • 6
  • 85
  • 105
1

your code should be fine. for example,

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.toString();

if you console it, you will see the result like this Banana,Orange,Apple,Mango

but if you want replace the commas(,) and replace it with Blank space

suppose,

var punctuated = "Banana,Orange,Apple,Mango"
punctuated.split(",").join(" ");

the result will be Banana Orange Apple Mango

to learn more about replace from string you can follw this link

Sadid Khan
  • 1,836
  • 20
  • 35