Store value in the given variable through functions [duplicate] - javascript

This question already has answers here:
Pass variables by reference in JavaScript
(16 answers)
Pointers in JavaScript?
(15 answers)
Closed 7 years ago.
Is there any how javascript or jquery to store values in the given variables while interacting through functions wuthout using global vars?
I made an example: http://jsfiddle.net/1fw2urks/3/
$(document).ready(function(){
start();
});
function start(){
var n = 0;
for (i = 0; i < 10; i++){
count(n);
}
alert(n);
}
function count(n){
countAgain(n);
}
function countAgain(n){
n += 1;
}
n var should come with value 10 if it worked, but it seems that we can't change the passed parameters value while in the function.
Is this affirmation right?
(I did some research and i found this answer, but, I am stubborn)

Related

Does the function call have access or retain the value of a local var variable of its parent function because I declare that variable as var? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Explanation of `let` and block scoping with for loops
(5 answers)
Closed 7 months ago.
I expect the calling of the first element in the array to print out the number 0. However, it prints 5. Does the function call have access to i of its parent function because I declare i as var? If I declare i as a let variable, it prints 0 as expected.
0-closureBug.js
function makeFunctionArray() {
const arr = []
for (var i = 0; i < 5; i++) {
arr.push(function() { console.log(i) })
}
console.log(i)
return arr
}
const functionArr = makeFunctionArray()
functionArr[0]()

Javascript Need a basic syntax explaination [duplicate]

This question already has answers here:
JavaScript by reference vs. by value [duplicate]
(4 answers)
Closed 2 years ago.
var x = 5;
function test2(x) {
x = 7;
}
test2(8);
alert(x);
Why exactly is this putting out the global var x=5, without being affected by anything within the function.
Because you passed a param called x, which is the same name as your global var. Try this out:
var x = 5;
function test2(y) {
x = y;
}
test2(8);
alert(x);

How the local variable inside a array of functions can be accessed outside of the block [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I have snippet below, where values become as array of functions. Each function contains a console.log(i); Where i was a local variable of the loop. However when i execute (values[1]()) the values array the console.log is printing the i values. I'm trying to understand how this is possible
var values = [];
for ( let i = 0; i < 2; i++ )
{
values.push(function() {
console.log(i);
})
}
console.log(values); /* output : Array [function() {
console.log(i);
}, function() {
console.log(i);
}]*/
values[0](); // output: 0
values[1](); // output: 1

Can't understand how this javascript code control flow? [duplicate]

This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
This is the code of which i am not able to understand the flow of and also how the value of i persist even after the for loop ends.
var printNumTwo;
for (let i = 0; i < 5; i++) {
if (i === 2) {
console.log("now");
printNumTwo = function() {
console.log("inside");
return i;
};
console.log(i);
}
console.log(i);
}
console.log(printNumTwo());
The output of the program is
0
1
now
2
2
3
4
inside
2
reason of this behavior is Closures
A closure gives you access to an outer function’s scope from an inner
function. In JavaScript, closures are created every time a function is
created, at function creation time. mozilla doc
var printNumTwo;
for (let i = 0; i < 5; i++) {
if (i === 2) {
console.log("now");
printNumTwo = function() {
console.log("inside");
return i;
};
console.log(i);
}
console.log(i);
}
console.log('now calling printNumTwo()')
let s=printNumTwo();
console.log('now printing return value of printNumTwo()')
console.log('retrun value of PrintNumTwo is:'+s);

for-loop not modifying a global variable? [duplicate]

This question already has answers here:
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 6 years ago.
I wrote a for-loop that for some reason refuses to modify a global variable. Instead, it seems to create a local variable that it modifies temporarily. A condensed version of my code is as follows.
var clubsArray = [obj, obj, obj];
//each obj contains a property of "goalsFor" which holds an integer
var madridTotalGoals = 0;
var barcaTotalGoals = 0;
function findTotalGoals(clubsArray, totalGoals) {
for(var i = 0; i < clubsArray.length; i++) {
totalGoals += clubsArray[i].goalsFor;
}
}
findTotalGoals(clubsArray, barcaTotalGoals);
// this loops properly and does the math, but it never changes the value of barcaTotalGoals
In the full code there are numerous arrays that hold "club" objects; each contain a property key "goalsFor", which hold an integer as a value. There are also numerous "totalGoals" variables (two are specified here) that have been declared globally.
Does anyone know why the global variable (e.g. barcaTotalGoals) is not being modified when passed through this function? When I console log each step of this loop, the math is taking place but the result is not being stored. I apologize if this has been asked before but I've searched thoroughly.
The variable you are trying to pass, is passed by value and not reference. So it wont affect the original variable
You can assign the value once the for loop is finished
function findTotalGoals(clubsArray, totalGoals) {
for(var i = 0; i < clubsArray.length; i++) {
totalGoals += clubsArray[i].goalsFor;
}
barcaTotalGoals = totalGoals;
}
You are passing by value instead of by reference...
Instead, you could try like this:
clubsArray = [obj, obj, obj];
var totalGoals = {
madrid: 0,
barca: 0
}
function goalsByCountry(clubsArray, totalGoalsClub) {
for(var i = 0; i < clubsArray.length; i++) {
totalGoals[totalGoalsClub] += clubsArray[i].goalsFor;
}
}
goalsByTeam(clubsArray, 'barca');

Categories