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);

};

6
  • 3
    Creates a string in my console... what's your actual output? Commented Jul 12, 2018 at 15:11
  • 3
    Your output is a string... it's just comma separated. Commented Jul 12, 2018 at 15:13
  • 2
    yeah it's a string, if you do "console.log("Should be a string: " + typeof underlinedStringNew);" it returns a string Commented Jul 12, 2018 at 15:13
  • 1
    Indeed, when I run this I get a string, just separated by commas. Commented Jul 12, 2018 at 15:14
  • 1
    Are you looking to return the string without the commas? Commented Jul 12, 2018 at 15:14

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

@Benisburgers You are welcome. Dont forget to accept this answer if it solved your problem. :)
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.