PDA

View Full Version : simple(?) javascript problem?


aznsmurf8
10-09-2007, 11:20 PM
I am currently learning javascript in school and have been assigned a problem in class that requires a number input, and the program will determine whether the number is binary or not. Here is what i have so far:

for (i = 0; i < input.length; i++){

if(input.charAt(i) != 1 && input.charAt(i) != 0){
document.BinaryForm.OutputText.value = "Invalid binary number"
validinput = false;

}else{
document.BinaryForm.OutputText.value = ""
validinput = true;}

It works... partially except when the non-binary digit goes at the end. (like 1002)
also fails on a number like 20001002000100
oops, yahoo had some issues with some of that code, but where it says OutputText, there's a .value after it, not a "..."

joec_11
10-09-2007, 11:36 PM
maybe you are not reading the last digit, should your for loop be i<= input.length