How do I go about converting an array with multiple strings to a single string using .join?

var ManyAnt =['ant 123,' 'ant 234', 'ant 345' 'ant456']

use arr.join(j)

where j is a string that refers to how you want the array elements to be joined, for example if you want to join them using spaces inbetween the elements

ManyAnt.join(' ') will do

var ManyAnt =['ant 123', 'ant 234', 'ant 345', 'ant 456'];
var singleString = ManyAnt.join(''); // creates 'ant 123ant 234ant 345ant 456'
var singleStringWithSpaces = ManyAnt.join(' '); // creates 'ant 123 ant 234 ant 345 ant 456'

How would you return this in a function?
I am required to turn something from a string, into an array, sort it, and return a number.
Here is my example:

var ManyAnt = 'ant 123, ant 234, don 123, ant 345, don 345,  ant 456'; 
function countAllFromAnt(antX) {
console.log(antX); //display the initial string (assuming this is running in the background)
  var afterForLoop = []; ///this is the output after the sorted ant list
 var stringOrig = antX.split(',_'); ///this is removing based on the commas and turning it into an array with many strings i.e. ['xxx', 'xxx']
  for (var i = 0; i <stringOrig.length;i++) { ///this is initiating the counter at position zero of the array and counting up until the length of the array is reached by one 
    if (stringOrig[i].startsWith(AntX)){ ///this sorts by Antxxx and turns it into list; I believe this is a string, a list.
  afterForLoop.push(stringOrig[i])   ///this is turning it into a string. I think this is incorrect, I want to count the item in the now sorted list that is pushed to afterForLoop.

console.log (afterForLoop); //this shows what happens with afterForLoop
console.log (afterForLoop.join(''); this shows a string of the list we created and could be incorrect
 return afterForLoop.length //returns the amount of items in the final sorted array
///what do I return above?

assert.equal(antX);  ///confirms if the parameter output is what we expect


UPDATE 2: I have written pseudo-code for what happens at each step

That depends on what you are wanting to return. Can you explain exactly what should be returned by this function?

I have explained above exactly what I want to do by using code instead of just describing.

First you need to understand, you can not split an array like:

var stringOrig = antX.split(',');

The split function is used to split a string into an array. antX is already an array.

I assume based on your code comment that you are trying to return the number elements in the array which start with the word ‘ant’? If that is not what you want, then please explain exactly what you want, because it is not clear to me from your code comment.

Thats what I meant. I may have laid out the question wrong.
let me edit it

I have now updated the problem (the actual code).

Based on your latest edit, the following is not a valid string:

var ManyAnt = 'ant 123', 'ant 234', 'ant 345', 'ant 456';

Did you mean the following?

var ManyAnt = 'ant 123, ant 234, ant 345, ant 456';

If so, you might also want to change the title of your topic to better reflect what you are trying to do. You are no longer converting an array with multiple strings into a single string.

Yes, I changed it now. Sorry for that basic syntax error. I did it before in my mind and not in practice haha

Im not sure my othe reply went through but I changed the syntax yes.

You should always start with a written algorithm before coding anything. The algorithm would look something like:

  1. Initialize a counter with value 0, to keep track of the number of words starting with ‘ant’

  2. Split antX string into an array using ', ’ as a delimter

  3. Iterate through the created array and check if each element starts with ‘ant’. If it does, then increment counter.

  4. Return counter

See if you can use the above algorithm to code your solution.

Yes I did that. I wrote it all out but I’m still getting used to the built in functions like.length in applied situations. I’m trying to return the number of antxxx

When you think you have written code that matches the algorithm I wrote above, then post it and we can take a look at it.

I have commented in my thought process.

Do you want me to post it seperately?

You split delimiter is incorrect and so is your join. In the string, there is a comma plus a space between each word/number combo. Your delimiter is only splitting on the comma.

Heading out for a run, so it will be a few hours before I am back online. I have posted a possible solution below based on what I believe you want in case you do not figure it out on your own. Try not to look at it until you have solved it yourself.

var ManyAnt = 'ant 123, ant 234, ant 345, ant 456';
function countAllFromAnt(antX) {
  var counter = 0;
  var filteredArr = [];
  var arrWords = antX.split(', ');
  for (var i = 0; i <arrWords.length;i++) {
    if (arrWords[i].startsWith('ant')){
  console.log(filteredArr); // new array
  console.log(filteredArr.join(', ')); // string of array
  return counter;

I changed the delimiter to be according to your specifications (also because I should have noticed that!)