1
 var i=0;
 function mul()
 {
    var qty = document.getElementsByClassName("qty");
    var rs = document.getElementsByClassName("rs");
    var amt = document.getElementsByClassName("amt");
    var mul=(qty[i].value)*(rs[i].value);
    amt[i].setAttribute("value",mul);
   sp.appendChild(iteminp);
   sp.appendChild(qtyinp);
   sp.appendChild(rsinp);
   sp.appendChild(amtinp);
   i++;
}

In the above program i want the value of 'i' should be increased for each time we call the function and it should be same all over the program like static variable. how to do it?

4
  • 2
    Whats the issue? Commented Dec 13, 2017 at 11:35
  • 2
    Exactly that way. It's global, so every function will see the same i. Commented Dec 13, 2017 at 11:35
  • 1
    just drop the var before i . Then i would be a global variable Commented Dec 13, 2017 at 11:37
  • There's nowhere there for i to be considered static. Do you simply want a global variable? If yes then declare it as window._i = 1;, but this seems like a bad way to achieve something. (It's common practice to prefix global vars with an underscore, and certainly don't just call it i) Commented Dec 13, 2017 at 11:50

3 Answers 3

2

JS variables are local to a function or they are global. Since you declared i outside a function, it's global.

To prove this, save this as test.html, open it in your browser and press the button a few times. I simplified your function a bit!

<script>
var i=0
function mul(){
  alert("i: " + i++)
}
</script>
<button onclick='mul()'>Press me</button>
Sign up to request clarification or add additional context in comments.

7 Comments

no , you see the above code in that i did like that but, for the second time if i call the function it is showing a error that " Uncaught TypeError: Cannot read property 'value' of undefined "
I think that error message is coming from this line: (qty[i].value)*(rs[i].value) - if qty[i] or rs[i] don't evaluate to something (because qty or rs has nothing at element i) then you will get that error. So I think that the problem is not related to i being global.
yeah, you are right the error is in that line, but what is the problem ?
when i run it for first time it is successfully executed but for the second time it is showing like that,but for the second time i am supplying a value,but i don't know why it is showing like that an how to solve it. can you please help to solve this?
can you give your num to contact?
|
2

You can have a javascript file named globals.js(don't forget to include it in your index.html) then declare your global variable, which will be available in all your web solution.

globals.js:

var _i = 0; // Would not recomend using "i" as the variable name since it 
            // can polute the global scope

yourFile.js:

 function mul()
 {
   var qty = document.getElementsByClassName("qty");
   var rs = document.getElementsByClassName("rs");
   var amt = document.getElementsByClassName("amt");
   var mul=(qty[_i].value)*(rs[_i].value);
   amt[_i].setAttribute("value",mul);
   sp.appendChild(iteminp);
   sp.appendChild(qtyinp);
   sp.appendChild(rsinp);
   sp.appendChild(amtinp);
   _i++;
}

1 Comment

its like the same,its not working.can you please tell me any other way to solve this?
2

You could create a decorator function that can wrap your function with a counter that will give you access to the count through a read only property.

This solution will require modern browsers due to having used Symbol, but you could substitute this with a dunder property eg. __count__ in it's place.

// this is used to create a privaty property on your function
const COUNT = typeof Symbol !== 'undefined' ? Symbol('count') : '__count__'

// this function takes your function and wraps it with a counter
function counter(fn) {
  // this is called whenever you call the decorated function
  function _counter(...args) {
    // increment the counter
    _counter[COUNT]++
    // call the original function
    return fn(...args)
  }
  // create the private property on your function and the accessor
  Object.defineProperties(_counter, {
    [COUNT]: { value: 0, writable: true },
    name: { value: fn.name || 'counter' },
    count: { get: () => _counter[COUNT] }
  })
  // return the decorator function
  return _counter
}

// function to decorate
function _mul(x, y) {
  // do something to multiply
  return x * y
}

// decorate your functions with the counter
const mul = counter(_mul)
const mul2 = counter(_mul)

// proof that both counters are independent and work correctly
console.log(
  mul(1, 2), // 2
  mul(3, 4), // 12
  `mul has been called ${mul.count} times`
)
console.log(
  mul2(5, 6), // 30
  mul2(7, 8), // 56
  mul2(9, 10),// 90
  `mul2 has been called ${mul2.count} times`
)
<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>

3 Comments

i can't understand this , can you please suggest me a edit in my above program
can you give you mobile number to contact?
Hi, sorry I did not reply. The function I provided wraps your function. it will attach a counter property to it that you can access using mul.count when you need to know how many times it had been called. This is the best solution because you are not polluting the global namespace with a variable. especially the use of i will bite you later as that is used in for loops by convention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.