Karl_vbulletin3_import16899
New Member
I want to convert a string to integer. I know that there are built-in functions to do it, but still i want to know why is this function not working:
JS: - saved as \[code\]js1.js\[/code\]\[code\]function atoi(str) { l = str.length; s2 = "0" for(i=0;i<l;i++) { if(str.charAt(i) != '1' || str.charAt(i) != '2' || str.charAt(i) != '3' || str.charAt(i) != '4' || str.charAt(i) != '5' || str.charAt(i) != '6' || str.charAt(i) != '7' || str.charAt(i) != '8' || str.charAt(i) != '9' || str.charAt(i) != '0') { break; } s2 = s2.concat(str.charAt(i)); } return Number(s2); }\[/code\]HTML:\[code\]<html> <head> <script src="http://stackoverflow.com/questions/14053733/js1.js"> </script> <Script> function printnum() { n = atoi(document.getElementById('numtxt').value) document.write(n); } </script> <title> Test JS1 functions </title> </head> <body> <input type="text" id="numtxt"> <input type="button" onclick="printnum()"> </body></html>\[/code\]Thank You.
JS: - saved as \[code\]js1.js\[/code\]\[code\]function atoi(str) { l = str.length; s2 = "0" for(i=0;i<l;i++) { if(str.charAt(i) != '1' || str.charAt(i) != '2' || str.charAt(i) != '3' || str.charAt(i) != '4' || str.charAt(i) != '5' || str.charAt(i) != '6' || str.charAt(i) != '7' || str.charAt(i) != '8' || str.charAt(i) != '9' || str.charAt(i) != '0') { break; } s2 = s2.concat(str.charAt(i)); } return Number(s2); }\[/code\]HTML:\[code\]<html> <head> <script src="http://stackoverflow.com/questions/14053733/js1.js"> </script> <Script> function printnum() { n = atoi(document.getElementById('numtxt').value) document.write(n); } </script> <title> Test JS1 functions </title> </head> <body> <input type="text" id="numtxt"> <input type="button" onclick="printnum()"> </body></html>\[/code\]Thank You.