英文:
going through an array with forEach() , how does the code understand that a parameter is index and another is the element value?
问题
The first code you provided is working because in the array.forEach
loop, the function addHundredIfDivisionBY3Possible
is called with two arguments: element
and i
. The first argument, element
, represents the current value in the array, and the second argument, i
, represents the index of that value in the array.
In your first code, you correctly use the element
as the value and i
as the index. Here's the relevant part:
function addHundredIfDivisionBY3Possible (element, i) {
if (element % 3 === 0) {
array[i] = (element += 100);
}
}
You check if the element
is divisible by 3, and if it is, you update the value in the array
at index i
.
In your second code, you have swapped the usage of element
and i
, which is why it doesn't work as expected. Here's the relevant part of the second code:
function addHundredIfDivisionBY3Possible (element, i) {
if (i % 3 === 0) {
array[element] = (i += 100);
}
}
In this code, you are trying to use i
as the value and element
as the index, which is not the correct way to interact with the array elements. The program understands which parameter means what based on the order in which they are provided to the function.
To summarize, the first code is working correctly because you are using element
for values and i
for indices, as intended. Swapping them in the second code results in incorrect behavior because it doesn't follow the expected convention for handling array elements.
英文:
Greeting guys , please take a look at this code
var array = [12, 929, 11, 3, 199, 1000, 7, 1, 24, 37, 4,
19, 300, 3775, 299, 36, 209, 148, 169, 299,
6, 109, 20, 58, 139, 59, 3, 1, 139];
function addHundredIfDivisionBY3Possible (element , i){
if (element % 3 === 0) {
array[i]= (element +=100) ;
}
}
array.forEach(addHundredIfDivisionBY3Possible);
console.log (array);
output:
[ 112, 929, 11, 103, 199, 1000, 7, 1, 124, 37, 4, 19, 400, 3775, 299, 136, 209, 148, 169, 299, 106, 109, 20, 58, 139, 59, 103, 1, 139 ]
the question is how did the code understand that the "element" parameter is the value and i is for index , supposedly i meant it the other way around and i wrote the function like this
// wanting the parameter 'element' to be the index and the parameter i to be the value of the element
function addHundredIfDivisionBY3Possible (element , i){
if (i % 3 === 0) {
array[element]= (i +=100) ;
}
}
why is the first one working and the second one is not , it may have to do to with me trying to index the element un-properly " array[element] " but the question remains .. how does the program understand which parameter mean what ?
tried looking it up on google
答案1
得分: 1
// 这是 forEach 的工作原理(有点类似)
function forEach(array, callback) {
for (let i = 0; i < array.length; i += 1) {
// 首先传递元素
// 其次传递索引
callback(array[i], i)
}
}
forEach(['a', 'b', 'c', 'd'], (e, i) => console.log('元素', e, '索引', i))
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
// this is how forEach works (kind of)
function forEach(array, callback) {
for (let i = 0; i < array.length; i += 1) {
// element is passed first
// index is passed second
callback(array[i], i)
}
}
forEach(['a', 'b', 'c', 'd'], (e, i) => console.log('element', e, 'index', i))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论