What the fucking fuck is this piece of shit "code"?
var numbers = [1, 2, 3, 4, 5], sum = 0;
forEach(numbers, function(number) {
sum += number;
});
console.log(sum);
// → 15
Where the hell does the number variable come from?
What the fucking fuck is this piece of shit "code"?
var numbers = [1, 2, 3, 4, 5], sum = 0;
forEach(numbers, function(number) {
sum += number;
});
console.log(sum);
// → 15
Where the hell does the number variable come from?
Other urls found in this thread:
developer.mozilla.org
dojotoolkit.org
developer.mozilla.org
anyforums.com
twitter.com
it's a parameter for the anonymous function used by forEach
dumb frogposter
it's a function parameter for the anonymous function callback
function(number){
sum += number
}
you probably arnt a programmer so ill try to explain this in words you understand.
the user function "function" is being declared in the call to ths function "forEach". like any function definition the arguments to the function have names and the name of its argument is being declared as "number". user function are special and they have access to variables from the surrounding scope thus sum does not need to be passed as an argument. "forEach" on ths otherhand iterates through the array "numbers" passing each element to the function "function".
hope that helps
Explain it in C and not some webdev shit
basically has an accumulator and the value (list of nums) being passed with a function to it (+)
print $ foldl (+) 0 [1..5]
so that does
(1+(2+(3+(4+(5+0)))))
>hope that helps
upvoted
Why the fuck does the Mozilla javascript docs state something completely different?
developer.mozilla.org
Nowhere is it mentioned that the forEach function may take an array as a parameter
might be a framework, I know that Dojo has a similar syntax to what OP posted
dojotoolkit.org
>explain anonymous functions in c
your a fucking idiot
explaining it in terms of function pointers used to emulate first class functions in no way helps op understand the code.
P.S. ive never done web development
explain it in binary not some high level shit
explain it in electrons not some high level shit
explain it in wavefunctions, not some high level shit
For many decades, other languages have slowly been stealing functional concepts and implementing them in an extremely poorly thought out fashion.
These languages, called "multiparadigm", are invariably just procedural kitchen sink languages.
Everyone in this thread is a moron
I'm sorry user, but the ones who don't know Javascript very well aren't the morons
It's one of the easiest languages to learn. I can almost guarantee you no one in this thread except me is employed.
.. as a web developer.
see pic related
>let's mix up types of a weak-typed language
>wow, look at the strange results I get!
these are non-issues if you aren't a moron.
>>wow ok so the language is bad whats your point
Still not as bad as java and .net
It's worse.
Java and .NET are better than Javascript
Java is literally better than your language
.NET is literally better than your language
How do you live with yourself?
Even weakly-typed languages can have consistency. The problem isn't that the results are "strange," it's that they're strange AND inconsistent. Why should ('5' + 0) be any different than ('5' - 0)? It shouldn't. In fact, why are the operations even allowed in the first place? Just do what Python does and say "why the fuck are you trying to concatenate an integer to a string" or "why the fuck are you trying to subtract from a string?" Neither operation makes any damn sense in the first place.
I can tell I'm right because I triggered you.
My main language is golang and I get paid 200k a year to write network software. Golang is objectively better than Java and .NET. It has objectively better performance and it's objectively more concise.
>I can tell I'm right
>CTRL + F "Javascript" in his post
>no results
>Why should ('5' + 0) be any different than ('5' - 0)?
Because '+' doesn't necessarily mean addition. It can mean concatenation.
>if err != nil
>if err != nil
>if err != nil
>if err != nil
>if err != nil
>'5' + 0 - 0
>50
you don't use forEach like that normally, it can just be a library or a different implementation cause whoever wrote the code is not a skilled programmer fo sho
str + str = concatenation
int + int = addition
Since they do completely different things, why even allow int+str or str+int?
oh hey man, another employed guy on Sup Forums! how does it feel? I'm a senior full stack dev working remotely and livin' the dream
"You have won" + int + " times."
simply because you're looking at the official documentation but you're using something else in that code..
"You have won $int times"
I can easily tell that it is iterating through the array and adding to sum, but I don't get the syntax or intent of the forEach() statement
what language is this?
Ends up being much messier than C or Python style string formatting.
It's javascript. The forEach functon iterates over the array and you pass a function as argument, that takes a a single element of the array, the current index and the array as a whole as parameters. The function then gets executed each iteration
developer.mozilla.org
Explicit control flow is good.