Value of global variable isn`t changing although its local value is changing

Multi tool use
Multi tool use












0














The value of the global variable currentLength is changed by entering different values to input. It is necessary that the value of the global variable currentLength be equal to the current value in input.



Entering the number 120, alert should work, it does not work, which means that the value of the global variable isn`t redefined.






function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">





I expect currentLength value is 120, but its all the time 0, be it alert or just using ofcurrentLength` value for further global operations, it makes nothing.










share|improve this question




















  • 3




    Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
    – Chris G
    2 days ago












  • You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
    – Chris G
    2 days ago










  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
    – Alex
    2 days ago










  • What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
    – Alex
    2 days ago


















0














The value of the global variable currentLength is changed by entering different values to input. It is necessary that the value of the global variable currentLength be equal to the current value in input.



Entering the number 120, alert should work, it does not work, which means that the value of the global variable isn`t redefined.






function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">





I expect currentLength value is 120, but its all the time 0, be it alert or just using ofcurrentLength` value for further global operations, it makes nothing.










share|improve this question




















  • 3




    Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
    – Chris G
    2 days ago












  • You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
    – Chris G
    2 days ago










  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
    – Alex
    2 days ago










  • What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
    – Alex
    2 days ago
















0












0








0







The value of the global variable currentLength is changed by entering different values to input. It is necessary that the value of the global variable currentLength be equal to the current value in input.



Entering the number 120, alert should work, it does not work, which means that the value of the global variable isn`t redefined.






function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">





I expect currentLength value is 120, but its all the time 0, be it alert or just using ofcurrentLength` value for further global operations, it makes nothing.










share|improve this question















The value of the global variable currentLength is changed by entering different values to input. It is necessary that the value of the global variable currentLength be equal to the current value in input.



Entering the number 120, alert should work, it does not work, which means that the value of the global variable isn`t redefined.






function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">





I expect currentLength value is 120, but its all the time 0, be it alert or just using ofcurrentLength` value for further global operations, it makes nothing.






function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">





function getCurrentLength() {
let currentLength = 0;
let output = document.getElementById('output');

calcLength();

function calcLength() {
let total = +output.value;
let len = document.getElementsByClassName('len');
for (let i = 0; i < len.length; i++) {
len[i].addEventListener('input', function() {
let value = 0;
currentLength = +len[i].value;
for (let j = 0; j < len.length; j++) {
let num = +len[j].value || 0;
value += num;
}
output.value = total + value;
})
}
}

if (currentLength === 120) {
alert(currentLength)
}
}
getCurrentLength();

<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" placeholder="length" class="len">
<input type="text" id="output" placeholder="output">






javascript ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Wais Kamal

1,7041519




1,7041519










asked 2 days ago









Alex

1135




1135








  • 3




    Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
    – Chris G
    2 days ago












  • You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
    – Chris G
    2 days ago










  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
    – Alex
    2 days ago










  • What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
    – Alex
    2 days ago
















  • 3




    Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
    – Chris G
    2 days ago












  • You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
    – Chris G
    2 days ago










  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
    – Alex
    2 days ago










  • What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
    – Alex
    2 days ago










3




3




Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
– Chris G
2 days ago






Your if(currentLength === 120) block runs exactly once at the very start, then never again. So even if currentLength ends up being 120, the alert won't show. currentLength does change its value all the time, it's always one digit behind what I type in the first input for instance. How is this supposed to work exactly? Is this about adding all input values together?
– Chris G
2 days ago














You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
– Chris G
2 days ago




You also have two functions but you only call them once, which makes it kinda superfluous to have a function in the first place. I tried to clean up the code: jsfiddle.net/khrismuc/rsdgc503
– Chris G
2 days ago












I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
– Alex
2 days ago




I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations
– Alex
2 days ago












What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
– Alex
2 days ago






What concerns the code above: 1. Result of all input values isn`t a current result, it summarizes all entered values between each other. 2. I have several mathematical operations that have result used each for next mathematical operations, well, I do it using functions otherwise it will be very unstructured...
– Alex
2 days ago














1 Answer
1






active

oldest

votes


















1














The reason is quite simple: You check currentLength === 120 once when calling getCurrentLength, but at that time, there is no currentLength, because currentLength only gets set when an input field changes.



Change your currentLength === 120 check to be inside the event handler, then you will get the expected result:



// ...
output.value = total + value;
if (currentLength === 120) {
alert (currentLength);
}
// ...





share|improve this answer





















  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
    – Alex
    2 days ago










  • Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
    – NikxDa
    2 days ago










  • My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
    – Alex
    2 days ago












  • When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
    – NikxDa
    2 days ago










  • 1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
    – Alex
    2 days ago













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944987%2fvalue-of-global-variable-isnt-changing-although-its-local-value-is-changing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The reason is quite simple: You check currentLength === 120 once when calling getCurrentLength, but at that time, there is no currentLength, because currentLength only gets set when an input field changes.



Change your currentLength === 120 check to be inside the event handler, then you will get the expected result:



// ...
output.value = total + value;
if (currentLength === 120) {
alert (currentLength);
}
// ...





share|improve this answer





















  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
    – Alex
    2 days ago










  • Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
    – NikxDa
    2 days ago










  • My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
    – Alex
    2 days ago












  • When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
    – NikxDa
    2 days ago










  • 1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
    – Alex
    2 days ago


















1














The reason is quite simple: You check currentLength === 120 once when calling getCurrentLength, but at that time, there is no currentLength, because currentLength only gets set when an input field changes.



Change your currentLength === 120 check to be inside the event handler, then you will get the expected result:



// ...
output.value = total + value;
if (currentLength === 120) {
alert (currentLength);
}
// ...





share|improve this answer





















  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
    – Alex
    2 days ago










  • Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
    – NikxDa
    2 days ago










  • My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
    – Alex
    2 days ago












  • When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
    – NikxDa
    2 days ago










  • 1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
    – Alex
    2 days ago
















1












1








1






The reason is quite simple: You check currentLength === 120 once when calling getCurrentLength, but at that time, there is no currentLength, because currentLength only gets set when an input field changes.



Change your currentLength === 120 check to be inside the event handler, then you will get the expected result:



// ...
output.value = total + value;
if (currentLength === 120) {
alert (currentLength);
}
// ...





share|improve this answer












The reason is quite simple: You check currentLength === 120 once when calling getCurrentLength, but at that time, there is no currentLength, because currentLength only gets set when an input field changes.



Change your currentLength === 120 check to be inside the event handler, then you will get the expected result:



// ...
output.value = total + value;
if (currentLength === 120) {
alert (currentLength);
}
// ...






share|improve this answer












share|improve this answer



share|improve this answer










answered 2 days ago









NikxDa

2,82111531




2,82111531












  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
    – Alex
    2 days ago










  • Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
    – NikxDa
    2 days ago










  • My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
    – Alex
    2 days ago












  • When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
    – NikxDa
    2 days ago










  • 1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
    – Alex
    2 days ago




















  • I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
    – Alex
    2 days ago










  • Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
    – NikxDa
    2 days ago










  • My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
    – Alex
    2 days ago












  • When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
    – NikxDa
    2 days ago










  • 1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
    – Alex
    2 days ago


















I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
– Alex
2 days ago




I need current value (that is inside an event listener) to be assigned to global variable (that is outside of event listener). Value of global variable is used for further mathematical operations, well, I need it anyway.
– Alex
2 days ago












Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
– NikxDa
2 days ago




Yes, you are assigning the global variable. But your if check runs only once at the beginning, where it cannot be fulfilled yet. So either run it inside the event handlers, or add a button to run the check. With your current implementation, your currentLength === 120 check will run once and fail.
– NikxDa
2 days ago












My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
– Alex
2 days ago






My level isn't so good, but I guess that it can be possible to do with callbacks or maybe one more event handler, isn`t it?
– Alex
2 days ago














When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
– NikxDa
2 days ago




When exactly do you want your alert to pop up? To understand your program flow, I will need more details.
– NikxDa
2 days ago












1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
– Alex
2 days ago






1. function getCurrentLength() is called 2. I can freely take value of currentLength for manipulations with it on the levels higher. All full code, if you need, here - codepen.io/alexseveneight/pen/MZWJjR Code chaining there is: calcLength() is one of code samples that gives me needed parameter. There are several functions there with outputting parameters. Then, calling function globalCalc(), I get all parameters for further operations.
– Alex
2 days ago




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53944987%2fvalue-of-global-variable-isnt-changing-although-its-local-value-is-changing%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Qf0CvtNZY C63SqJuCz pwAT8BuJ5CP O3DF11Otmx52QUWs jw0MXjRunw,i kWrvUfHHXddRk673YC,F,m4cQtzGdk,2AjI
kT73CBDmzOQi,sMm21 Ya4E,shKJroPW8aJWvyYh0w Vb,E2,vLzHyT2

Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas