Answer the question
In order to leave comments, you need to log in
How to extract the value from an anonymous function to a global variable?
There is an anonymous function, you need to pull out a variable from it for further use
let np;
window.onclick = function(e){
var elem = e ? e.target : window.event.srcElement;
np = elem.id + "";
alert(np);
return np;
};
console.log(np);
Answer the question
In order to leave comments, you need to log in
The construction for (oneNumber in myNumbers)
actually means the following:
Go through all the keys of the array in turn myNumbers
and at each step put the value of the key into the variable oneNumber
. After that, the addition occurs myNumbers[oneNumber]
, that is, the values in the array myNumbers
by index oneNumber
, to the variable total
.
in other words
, here is the equivalent entry to your
for loop (var oneNumber in myNumbers) {
total = total + myNumbers[oneNumber];
}
In each iteration of the loop, the oneNumber variable is created and the element from the myNumbers array is written there
oneNumber
forgot to announce.
You should add a line abovevar oneNumber;
Read about the for in construction https://developer.mozilla.org/ru/docs/Web/JavaScri...
In this case, oneNumber is the index of an element in an array, it can be called anything, even i, even value , even oneNumber.
The for in loop iterates through all the elements of the array in turn, takes the corresponding element and adds to the total variable. As soon as all elements are enumerated, the following function will work - document.write (total);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question