0

Ultimately I am prompting the user for a guess, which is then ultimately changed so regardless of what the user inputs it will always Capitalize the first letter and make the rest lowercase. (Im doing this so if the user types in a guess the string will either match or not match the values in an array.) I tried doing a for statement to use a loops counter (3 total guesses is what im looking for). But when I try to use a indexOf to check the array, I keep getting an "unexpected token" error on that line that contains the indexOf statement. So the question would be (1) what am i doing wrong in this line of code?

//declare variables
var sportsArray = new Array("Football", "Basketball", "Rollerblading",   "Hiking", "Biking", "Swimming");
var name = prompt("Enter your name");
var loops = 0;
var score = 0;
var sGuess = prompt("enter your sport guess");

// uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.

var sFirstPart = sGuess.substr(0, 1);
var sFirstCap = sFirstPart.toUpperCase();
var sSecondPart = sGuess.substring(1, sGuess.length);
var sSecondLow = sSecondPart.toLowerCase();
var usableGuess = sFirstCap + sSecondLow;

while(loops < 4){

    if(sportsArray.indexOf(usableGuess) = 0 { 
        document.write("nice guess");
        loops++;
    }else { 
        document.write("loser");
        loops++;
    }
}
Nate E.
  • 125
  • 1
  • 1
  • 10

2 Answers2

1

This works for checking the whole array:

var sportsArray = new Array("Football", "Basketball", "Rollerblading",   "Hiking", "Biking", "Swimming");
var name = prompt("Enter your name");
var loops = 0;
var score = 0;
var sGuess = prompt("enter your sport guess");

// uses substrings to ultimately capitalize the 1st letter, and make everything after it lowerCase.

var sFirstPart = sGuess.substr(0, 1);
var sFirstCap = sFirstPart.toUpperCase();
var sSecondPart = sGuess.substring(1, sGuess.length);
var sSecondLow = sSecondPart.toLowerCase();
var usableGuess = sFirstCap + sSecondLow;

while(loops < 4){

    if(sportsArray.indexOf(usableGuess) > -1) { 
        document.write("nice guess");
        loops++;
    }else { 
        document.write("loser");
        loops++;
    }
}

You'd want to use indexOf(guess) > -1 to check if the guess is present at any index of the array. For checking just one index position it would be indexOf(guess) == 0.

Anders Elmgren
  • 637
  • 5
  • 12
0

sportsArray.indexOf(usableGuess) === 0) instead of sportsArray.indexOf(usableGuess) = 0

It's a good practice to check for equality with constant on the left side. It will throw an exception in most browsers:

var a = 3;
if (12 = a) { // throws ReferenceError: invalid assignment left-hand side in Firefox
  //do something
}

Also: use tools that provide static code analysis. A jslint.com or jshint.com for js is a good choice. There are also IDE plugins explicitely for that (using either of those two and more), see Is there a working JSLint Eclipse plug-in?.

Community
  • 1
  • 1
Jan Chimiak
  • 736
  • 7
  • 20