Question on Parameters - Dice Game - Step 20

Tell us what’s happening:

If I’m correct, parameters in a callback function are supposed to influence the statement but I dont see how it influences them or why do statements need them in the first place. Such as with this, how does the parameter influence the dice.textContent being assigned to diceValuesArr[index]?

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36

Challenge Information:

Learn Intermediate Algorithmic Thinking by Building a Dice Game - Step 20

Hello:)
The forEach method calls a function for each element in an array. It is like a for loop.

const items = ["item1", "item2", "item3"];
const copyItems = [];

// before
for (let i = 0; i < items.length; i++) {
  copyItems.push(items[i]);
}

// after
items.forEach((item) => {
  copyItems.push(item);
});

Another example:

<script>
let text = "";
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);

document.getElementById("demo").innerHTML = text;
 
function myFunction(item, index) {
  text += index + ": " + item + "<br>"; 
}
</script>

As in the second example you can see, forEach takes myFunction that has two parameters. This is equal to this:

listOfAllDice.forEach((dice, index) => {
dice.textContent = diceValuesArr[index];}

or other way :

listOfAllDice.forEach((myFunction);

function myFunction(dice, index) {
dice.textContent = diceValuesArr[index];}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.