JavaScript - I Guess I'm Stupid?
Anyone know why my switchh statement isn't working?
Code: var rehash = inward.location.hash.match(/[^#]/); switch(rehash) { case 1: rehash = 5; break; case 2: rehash = 1; break; case 3: rehash = 2; break; case 4: rehash = 3; break; case 5: rehash = 4; break; case null: rehash = 5; default: rehash = null; }; Code: if (rehash == 1) { rehash = 5; } else if(rehash == 2) { rehash = 1; } else if(rehash == 3) { rehash = 2; } else if(rehash == 4) { rehash = 3; } else if(rehash == 5) { rehash = 4; } else if(rehash == null) { rehash = 5; } else{rehash = "Invalid URL or REFERRER"} the if string works fine -_- i must be missing something Similar Tutorialsimport java.util.Scanner; import java.util.Arrays; /*********************************************************** * WordGuess game is a Hangman like game for two (or more) * players. This is a two player version *The game of Hangman involves one player making a word and a second * player guessing the word by guessing each letter in turn. If the guessing *player cannot get it in 6 (or so depending on the version) tries, the other *player wins. If the guesser gets it in the requisite number of tries, then the *guesser wins. This is a variant of the Hangman game involving two players *guessing against each other. * * * @author JavaFish * @I can't seem to get my loop working....it's not behaving like i want it to. ***********************************************************/ public class WordGuess { private Scanner kb; // use for all keyboard entry private Player player1; // first player private Player player2; // second player private WordBank wordList; // the dictionary to use /* alternate instance variable for the players deactivate the individual players if you choose to use this version. */ // private Player[] players; /********************************************************* * Explicit value constructor that builds a random dictioinary * * @param player1 The name of player1 * @param player2 The name of player2 ********************************************************/ public WordGuess(String player1, String player2) { wordList = new WordBank(); wordList.getWord(); this.player1 = new Player(player1); this.player2 = new Player(player2); } /********************************************************* * Explicit value constructor that builds a seeded dictioinary * * @param player1 The name of player1 * @param player2 The name of player2 * @param seed The seed to pass to WordBank constructor ********************************************************/ public WordGuess(String player1, String player2, long seed) { wordList = new WordBank(); wordList.getWord(); this.player1 = new Player(player1); this.player2 = new Player(player2); } /********************************************************* * play game plays the game until one player reaches 10 wins *********************************************************/ public void playGame() { boolean loopControl; boolean loopControlOne; boolean loopControlTwo; boolean playerTurn; boolean startOver; String playerInput; char playerCheck; String checkProgress; String checkGuess; String word; char[] dashes; char[] wordLetters; char[] lettersGuessed; kb = new Scanner(System.in); startOver = true; loopControl = true; playerTurn = true; System.out.printf("Welcome to Word Guess %s and %s\n\n", player1.getName(), player2.getName()); if (loopControl) { startOver = true; word = wordList.getWord(); dashes = new char[word.length()]; wordLetters = new char[word.length()]; lettersGuessed = new char[6]; for(int i = 0; i < word.length(); i++) { dashes[i] = ('-'); wordLetters[i] = word.charAt(i); } System.out.print('-'); if(startOver) { checkGuess = new String(lettersGuessed); checkProgress = new String(dashes); loopControlOne = true; loopControlTwo = true; if(playerTurn) { loopControlOne = false; } if (loopControlOne) checkGuess = new String(lettersGuessed); checkProgress = new String(dashes); playerTurn = true; { for(int i = 0; i < word.length(); i++) { System.out.print(dashes[i]); } System.out.print("\nUsed letters: "); for(int i = 0; i < 6; i++) { if(lettersGuessed[i] != 0) { System.out.print(lettersGuessed[i] + " "); } } System.out.printf("\n%s, guess a letter. ", player1.getName()); // Gets the players input and assigns it to variable playerInput playerInput = kb.nextLine(); playerCheck = playerInput.charAt(0); playerInput = new String("" + playerCheck); System.out.println(); for(int i = 0; i < word.length(); i++) { if(playerCheck == wordLetters[i]) { dashes[i] = playerCheck; } } if(checkGuess.contains(playerInput)) { System.out.printf("%s is already guessed. %s, you lose your turn.\n", playerInput, player1.getName()); loopControlOne = false; } else if(word.contains(playerInput)) { for(int i = 0; i < word.length(); i++) { if(wordLetters[i] == playerCheck) { dashes[i] = playerCheck; } } if(checkProgress.contains("-")) { System.out.printf("Correct: %s go again.\n", player1.getName()); for(int i = 0; i < 6; i++) { if(lettersGuessed[i] != 0) { lettersGuessed[i] = playerCheck; i = 6; } Arrays.sort(lettersGuessed); } } else { System.out.printf("Correct. %s you win a point!\n", player1.getName()); System.out.println(word); player1.iWon(); System.out.printf("%s: %d\t%s: %d\n\n", player1.getName(), player1.getScore(), player2.getName(), player2.getScore()); startOver = false; if(player2.getScore() == 5 || player1.getScore() == 5) { System.out.print("Game over. %s wins!\n"); loopControl = false; loopControlTwo = false; } // Set up new loop that clears everything. } } else { System.out.printf("Incorrect. %s, you lose your turn.\n", player1.getName()); loopControlOne = false; for(int i = 0; i < 6; i++) { if(lettersGuessed[i] == 0) { lettersGuessed[i] = playerCheck; i = 6; } Arrays.sort(lettersGuessed); } } } if (loopControlTwo) { checkGuess = new String(lettersGuessed); checkProgress = new String(dashes); playerTurn = true; for(int i = 0; i < word.length(); i++) { System.out.print(dashes[i]); } System.out.print("\nUsed letters: "); for(int i = 0; i < 6; i++) { System.out.print(lettersGuessed[i] + " "); } System.out.printf("%s, guess a letter. ", player2.getName()); // Gets the players input and assigns it to variable playerInput playerInput = kb.nextLine(); playerCheck = playerInput.charAt(0); playerInput = new String("" + playerCheck); for(int i = 0; i < word.length(); i++) { if(playerCheck == wordLetters[i]) { dashes[i] = playerCheck; } } if(checkGuess.contains(playerInput)) { System.out.printf("%s is already guessed. %s, you lose your turn.\n", playerInput, player1.getName()); loopControlTwo = false; } else if(word.contains(playerInput) && checkProgress.contains("-")) { { System.out.printf("Correct: %s go again.\n", player2.getName()); for(int i = 0; i <6; i++) { if(lettersGuessed[i] != 0) { lettersGuessed[i] = playerCheck; i = 6; } } Arrays.sort(lettersGuessed); } } else if(word.contains(playerInput)) { System.out.printf("Correct. %s you win a point!\n", player1.getName()); System.out.println(word); player1.iWon(); System.out.printf("%s: %d\t%s: %d\n\n", player2.getName(), player2.getScore(), player1.getName(), player1.getScore()); loopControlTwo = false; if(player2.getScore() == 0 || player1.getScore() == 0) { System.out.print("Game over. %s wins!\n"); loopControl = false; } } else { System.out.print("Incorrect. %s, you lose your turn.\n"); loopControlTwo = false; for(int i = 0; i < 6; i++) { if(lettersGuessed[i] == 0) { lettersGuessed[i] = playerCheck; i = 6; } Arrays.sort(lettersGuessed); } } } } } } } pop up blocker is stopping my user action from using window.open. It is a html button that i want to open a dialog to tell the user his item was added to the shopping cart. The sumbit_cart() function is supposed to send a request via ajax then use window.open to open the dialog if the ajax function was successful. Code: <input type="button" value="Add to Cart" onclick="return submit_cart();" /> I can understand the pop up blocker from blocking random pop ups but this is a user triggered event i don't see why it isn't allowing it. What can i do to make this work? |