-1

I am a new to JavaScript. I have tried to use regular expression in ifcondition. Here is my code:

var location1 = 3;
var location2 = 4;
var location3 = 5;

var guess;
var hits = 0;
var guesses = 0;
var string = "";
var reg = /[0-6]/i;

var isSunk = false;

while (isSunk == false) {
guess = prompt("Ready, aim, fire! (enter a number 0-6): ");
if (guess != reg || guess ==string) {
    alert("Please enter a valid cell number!");
} else {
    guesses = guesses + 1;

if (guess == location1 || guess == location2 || guess == location3) {

        hits = hits + 1;
        alert("HIT!");

        if (hits == 3) {
            isSunk = true;
            alert("You sank my battleship!");
        }
    } else {
        alert("MISS!");
    }
}
}
var stats = "You took " + guesses + " guesses to sink the battleship, " +    "which means your shooting accuracy was " + (3 / guesses);
alert(stats);`

My problem is in this if (guess != reg || guess ==string) condition. It didn't work as I expected. I would like that function prompt allows only 0,1,2,3,4,5,6 numbers without any words and spaces. But in fact If I enter one of needed number (1 for example) it won't allow to execute further actions. I read a lot through stackoverflow about regular expression in Javascript (Convert the Regular expression, Regular expression, Basic regular expression) and more but I could not find the answer to my problem. So the question is:

How to set proper regular expression in if condition in JavaScript if it has already declared as variable?

3
  • You need to test your regular expression against its value, w3schools.com/jsref/jsref_regexp_test.asp Commented Feb 29, 2016 at 13:04
  • 1
    guess != reg here you compare a user string with a regex object; this is not correct, you need to reg.test(guess) You also need to anchor the regex /^[0-6]$/ to make it match 1 digit Commented Feb 29, 2016 at 13:05
  • Murtaza, actually, I have already done this here regexr.com and everything was alright Commented Feb 29, 2016 at 13:07

2 Answers 2

1

To test whether a string matches a regular expression you can't just compare them using the standard comparison operators. Something that should work is

if (reg.test(guess)) {
    ....
}

Also the regular expression [0-6]i also matches the string "hello1, since there is a one in it. What you want is probably something like ^[0-6]$. The test for the empty string is also unnecessary, by the way, since "" does not match that expression.

To explain: The ^ and the $ match the beginning and the end of the string, respectively. Framing a regular expression with these symbols essentially says that the whole string, not only a substring, has to match the expression.

Sign up to request clarification or add additional context in comments.

Comments

0

Regular expression are not the right tool here.

 guess = prompt("Ready, aim, fire! (enter a number 0-6): ");
 guess = +guess;   // Convert *guess* to number, sets to NaN if not numeric
 if (guess >= 0 && guess <= 6) {  // check if number is in range (NaN will fail)
     ....

Comments

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.