I have function in JavaScript:
function calc(num) {
if (num <= 22) {
return parseInt(num);
} else {
num += '';
var curr = 0;
for (var i = 0; i < num['length']; i++) {
curr += parseInt(num[i]);
};
return curr;
};
};
This function calculates new number like:
if I have number greater than 22, this function returns new number which is a sum of it’s subdigits (e.g. 28 > 22 => return (2+8) ).
This function works great in Firefox, but I’m getting “NaN” error in Internet Explorer with numbers greater than 22. So the problem must be in “else”.
What’s wrong?
You need to
num.charAt(i)as you cannot access string characters withString[offset]in IE.(
s = "qwe"; alert(typeof s[1] === 'undefined')istrue)