0

Thanks for taking the time to look at my problem. What I'm trying to do is create a javascript function that tests whether a sting is a particular length and also whether each element of that string can be found in another string. The function then needs to return a boolean value of either true or false depending on whether the string is valid.

Here's what I have:

N_ALPHA = 6;
N_CHOICES = 4;
ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var alphabet = ALPHABET.substring(0, N_ALPHA);

function isValidGuess(inStr)
{    var valid;
     var Str = inStr;

     for (i=0; i<Str.length; i++)
     {    if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES.length)
          {    valid = true;
          }
          else
          {    valid = false;
          }
     }  

     return valid;
}

This code is not working at all. It only returns false every time. Any help you could provide would be greatly appreciated. Thank you.

user2430969
  • 1
  • 1
  • 1

1 Answers1

0

N_CHOICES.length return undefined, because variable N_CHOICES is number. you have to change your condition to

if (Str.charAt(i) === alphabet.charAt(i) && Str.length == N_CHOICES)
berus97
  • 384
  • 1
  • 5