Join Strings with join

Join Strings with join
0

#1

Tell us what’s happening:
I have decided to give myself a challenge, in which I join the strings in an arrray into a single line string using the join(). However, I want to separate each array string elements with a comma (,), and last string in array with the word “and”.

Can someone look at the code i have written and help know where i went wrong?

Your code so far

var joinMe = ["Split","me","into","an","array"];
var joinedString = '';

// Only change code below this line.
var lastElement = joinMe.length - 1;
var total = joinMe.length;
var joinMEelement = 0;
//alert(check);
for (; joinMEelement < total; ) {
  
   if( joinMEelement !== lastElement) {
     joinedString = joinMe.join(', ');
     }
 // else {
  joinedString = joinMe.join(' and ');
 // }
 
  joinMEelement++;

}

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:50.0) Gecko/20100101 Firefox/50.0.

Link to the challenge:


#2

All you need is joinedString = joinMe.join(' ');


#3

this i already passed, and did. i just challenged myself to do extra to test my skill and understanding of concept


#4

I did not understand what you wanted as the final output.


#5

this is the final input i am looking to have “Split me into an and array” as final output.

BELOW ARE THE CODES I HAVE WRITTEN TO ACHIEVE THAT BUT IT DOESNT INSERT THE WORD “and” between the last two arrays of the string.

var joinMe = [“Split”,“me”,“into”,“an”,“array”];
var joinedString = ‘’;

// Only change code below this line.
var lastElement = joinMe.length - 1;
var totalElements= joinMe.length;
var joinMeElement = 0;
//alert(check);
for (; joinMeElement < totalElements; ) {

if( joinMe[joinMeElement] != lastElement) {
if (joinMe[joinMeElement] === lastElement) {
joinedString = joinMe.join(’ and ‘);
}
joinedString = joinMe.join(’, ');

joinMEelement++;

}

can you help me know where the problem is?


#6
var joinMe = ["Split","me","into","an","array"];
var joinedString = '';

var lastWord = joinMe.pop();
joinedString = joinMe.join(' ').concat(" and ").concat(lastWord);

You don’t need a loop.