Help needed with Test Your Skills: Arrays 3;

Hello everyone. I’m working on Arrays 3, and I’m stuck on the third exercise:

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.

My fiddle: https://jsfiddle.net/hassang97/2rt95efn/10/

(let me know if the link is broken).

My approach has been to loop over the array, and add the parenthesis besides each item in the array.

How can I find the index of each item in the array, and then loop over my array so as to add the index number besides each name.

Also, how can I put the index number for each item inside a parenthesis, and then add that to the Array.

I tried various things, including indexOf but that will only return the index of a specific item you are searching for, what if the array contains hundreds of items? I essentially want to find the final index, then starting at 0, loop over the array and add (i), incrementing by 1 each time until I reach the final item in the array.

Let me know if anything is unclear or if you have any questions! Thank you in advance.

1 Like

Hi there @Hassan_Garshasb! You are definitely on the right track with this.

I’ll give you a clue — forEach() is your friend!

2 Likes

Hi Chris! Thanks for your response. I’ve gotten mostly there, i wrote this forEach loop

array.forEach(function(element, i) {
array[i] = array[i] + ’ ’ + (i);
});

Which gives me the name + the index number besides it, but how can I get the index number inside the parenthesis? I googled around but with no luck.

Thanks in advance!!

You are definitely getting close. Tell you what, have a look at our answer to see what we did: https://github.com/mdn/learning-area/blob/master/javascript/introduction-to-js-1/tasks/arrays/marking.md#task-3

2 Likes

let me know what you guys think of my answer ?
whether it is the right or wrong?

let myArray = [ “Ryu”, “Ken”, “Chun-Li”, “Cammy”, “Guile”, “Sakura”, “Sagat”, “Juri” ];

myArray.pop();
myArray.push(“divya”,“dipen”);

for(x = 0 ; x < myArray.length; x++){
myArray[x] =${myArray[x]} (${x});
}

let myString = myArray.join("-");

1 Like

Hi @divyabk54, and welcome to our community!

Your answer works completely fine, well done!

One thing — I did have to add backticks round the template literal inside the for loop, as the code wasn’t working for me, but then I realised that Discourse had eaten them :wink:

In future, please consider putting your code into an online code editor like codepen, and then sharing the URL with us — this makes it much easier to test and debug, and it also avoids such problems.

All the best,

Chris

1 Like

This is how I did mine using forEach()

let myArray3 = [ “Ryu”, “Ken”, “Chun-Li”, “Cammy”, “Guile”, “Sakura”, “Sagat”, “Juri” ];

myArray3.pop();

myArray3.push(‘Alexander’, ‘Rengkat’);

let finalArry = [];

myArray3.forEach((myArr3, index) => {

let newArr = ${myArr3} (${index});

finalArry.push(newArr)

});

let jointhem = finalArry.join(’-’);

console.log(jointhem);