Assessment wanted for Arrays 3 - Test your skills

This is the question
For this final array task, we provide you with a starting array, and you will work in somewhat the opposite direction. You need to:

  1. Remove the last item in the array.
  2. Add two new names to the end of the array.
  3. Go over each item in the array and add its index number after the name inside parentheses, for example Ryu (0) . Note that we don’t teach how to do this in the Arrays article, so you’ll have to do some research.
  4. Finally, join the array items together in a single string called myString , with a separator of " - ".

My Code:
let myArray = [ “Ryu”, “Ken”, “Chun-Li”, “Cammy”, “Guile”, “Sakura”, “Sagat”, “Juri” ];
myArray.pop();
myArray.push(‘Alexander’, ‘Rengkat’);
for(var i=0;i<myArray.length;i++){
myArray[i] = myArray[i]+’ ‘+’(’+i+’)’;
}
let myString;
myString = myArray.join(’ - ');

//And the Output I get is:
Ryu (0) - Ken (1) - Chun-Li (2) - Cammy (3) - Guile (4) - Sakura (5) - Sagat (6) - Alexander (7) - Rengkat (8)

Please see my code and tell me whether I’m correct. Is this the required output.
Thanks in Advance!!!

Hello @Deepi_code

you ding great well done just 2 notice

  1. you can marge this ’ ‘+’(’ to be ’ (’
  2. it would be better to share link to the task or topic you asking about to know what you trying to do in future post

and have a nice day :slight_smile:

Thank You for replying!!!

you very welcome :slight_smile:

specifically what does this line mean.
"myArray[i] = myArray[i]+’ ‘+’(’+i+’)’; "

it is located under Deepi’s code; where he looped his array.

I would like to know what all of the pluses and things mean starting where he declared his array.

Hi @BenjiG178 and welcome to the community :wave:

You can use pluses to build strings in JavaScript from variable values an literal text.
This line

myArray[i] = myArray[i]+' '+'('+i+')';

essentially means: "Take the array value add a space, a parenthesis, the index and another parenthesis. Then save everything into to same variable. That’s how “Ryu” becomes the new string “Ryu (0)”.
In modern JavaScript you would rather use a template literal to make it easier to read:

myArray[i] = `${myArray[i]} (${i})`;

I hope that makes it clearer.
Michael

1 Like

Thanks very much!!
Im surprised my answer was replied to very fast

1 Like

@BenjiG178 I just saw it popping up while working on the HUGE backlog of open questions and thought: “I know the answer, let’s quickly write back.” :grin:

By the way: Your email address is visible in your post. It ended up in the optional “name” field of your profile. I recommend removing it.

1 Like