 # 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.

(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.

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.

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

2 Likes

Hi @divyabk54, and welcome to our community!

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

1 Like

Hi Chris, thanks for posting your solution. I have a question about the syntax in the .forEach solution.

I understand everything up until the myArray[index] =newElement portion of the code.

Why isn’t the variable declaration first (ie. newElement = myArray[index])

What is the myArray[index] portion of the code doing?

Would appreciate any light you can shed. Thank you!

Hello @wilford.amy

myArray already declared earlier on that line

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

``````let newElement = `\${ element } (\${index})`;
``````

and for this one

``````myArray[index]  = newElement;
``````

it set the array element at index to be newElement

hope that help and have a nice day 1 Like

That does help! Thank you.

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

myArray.pop();
myArray.push(‘shahhussain’,“bobo”);
for(let i=0;i<myArray.length;i++){

myArray[i] +=`(\${i})`

}
let myString=myArray.join("-");
// Don’t edit the code below here!

1 Like

Hello @Shah_Hussain

you doing great well done

and have a nice day Hey guys, here is my solution!

myArray.pop();
myArray.push(“Blanka”);
myArray.push(“Dallsing”);
for (let i = 0; i < myArray.length; i++) {
myArray[i] += `(\${[i]})`;
}

const myString = myArray.join(’ - ');

1 Like

Hello @Felipe_Vieira

you doing great well done you just need to add extra space between the index number and the element
so it will be
` myArray[i] += ` (\${[i]})` ;`

hope that help and have a nice day 1 Like

@chrisdavidmills Hello, i tried to do mine using the for…of loop. It doesn’t seem to give the proper result, could you help me?

here is what i tried:

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

myArray.push(‘Eddy’, ‘Marc’);

for(let name of myArray){
let dex = myArray.indexOf(name);
name = `\${name} (\${dex})`;
}
let myString = myArray;

Hi @Eddy_Marc_Henri_Ndonga and welcome to the community The `name` variable in the loop is temporary. you can’t change the array values with it. You need to assign the value to `myArray[dex]`.

Michael

Hello.
Here is my solution:
let finalArray = [];

``````for (let [i, array] of myArray3.entries()) {

let newArray = ` \${array}(\${i + 1}) `;

finalArray.push(newArray);

}

let myString3 = finalArray.join("-");

console.log(myString3);``````

Hi @milan.z and welcome to the community You’re code is correct. Well done!
The only thing that I would change is the variable `newArray`. It’s a bit misleading as it contains a string.

Have a nice day,
Michael

1 Like

Thanks, @mikoMK for the suggestion I gonna apply it.

1 Like