having problems with javascript code for class

Discussion in 'OT Technology' started by biawokauns, May 20, 2006.

  1. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    :rant:

    view project at:
    http://students.washington.edu/ngng/fit100/project2b/project2b.html

    objective:
    I'm trying to code the part where, if the current character entered is not equal to a letter in the secretword, to append the character in the "wrong guesses" section. Right now, I am confused why it displays in the wrong guesses section, even if the letter is correct :hs:

    thanks!

    PHP:
    //creates function "checkGuess"
    function checkGuess() {

        
    //alert('Running checkGuess() function');  

        //declares totalFound, sets equal to 0
        
    var found 0;

        
    //declare guessedLetter, and sets it to the value of the input box, sets it to upper case
        
    var guessedLetter document.getElementById("guess").value.toUpperCase();
        
        
        
    //reset value of guess ID
        
    document.getElementById('guess').value '';


        
    //for loop checks through all the previousGuesses
        //as long as i is less than the length of the previousGuesses
        
    for (0previousGuesses.lengthi++) {
        
            
    //if the current character index in teh for loop
            //equals the lettere guessed, an alert box is displayed
            //and found is set to -1
        
            
    if(previousGuesses.charAt(i) == guessedLetter){
                
    alert('You\'ve guessed the letter \'' guessedLetter '\' before!');
                
    found = -1;
            }
        } 
        
        
        
    //for loop that replaces our blank cells with the correct letter
        //or appends the wrong guess in the paragraph below using the innter.HTML
        
    if(found != -1){
            for (
    0secretWord.lengthi++) {
                    if(
    secretWord.charAt(i) == guessedLetter){
                        
    //gets the elemnent in current document with id "blank" + whatever the current index nubmer is
                        //then it replaces the underscore which we previously entered, and replaces it with
                        //the guessedLetter
                        
    document.getElementById('blank' i).innerHTML guessedLetter;
                        
    found++;
                        }  
                } 
                
            
    //if the current  character is not equal to the guessedLetter, then add letter to wrong guesses    
            
    if (secretWord.charAt(i) != guessedLetter) {
                
    document.getElementById('wrongGuesses').innerHTML += guessedLetter;
                
                
    //call moveProgressBar function            
                
    moveProgressBar();
        
            }    
        } 
        
        
    //add value of found to totalFound
        
    totalFound totalFound found;
        
        
    //check to see if the total letters found equals the secretWords length
        
    if(totalFound == secretWord.length){
        
    alert('Congrats, you\'re correct!  The secret word is ' secretWord);
        
    window.location.reload();
        }
        
        if(
    progressLocation == size){
        
    alert('You have used up all your guesses, we are restarting the game!');
        
    window.location.reload();
        }
        
        
    //appends current guess to previous guesses
        
    previousGuesses += guessedLetter;

        
    }

     
    Last edited: May 20, 2006
  2. ady

    ady New Member

    Joined:
    Mar 3, 2005
    Messages:
    10,959
    Likes Received:
    0
    Location:
    UK
    if (secretWord.charAt(i) != guessedLetter) {

    You can't use i here, it's greater than the length of the word as you've used it in your for structure.
     
  3. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    isn't that, the current index position of the secretWord (as it cycles through)?
     
  4. ady

    ady New Member

    Joined:
    Mar 3, 2005
    Messages:
    10,959
    Likes Received:
    0
    Location:
    UK
    I should of been more clear. I'm refering to the second if statement, it's outside your for loop.
     
  5. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    the problem is, when i move it inside, i get this shitstorm where it'll display the letter guessed X times, where X = to the number of characters in the secretword
     
  6. ady

    ady New Member

    Joined:
    Mar 3, 2005
    Messages:
    10,959
    Likes Received:
    0
    Location:
    UK
    You need to change it so that after the for has run, if the letter wasn't found then add it to the wrong guesses. At a quick look I think you can use the found variable for this, if it hasn't changed from 0 then add it to wrongguesses.
     
  7. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    thanks ady, i'm gonan fuck with it tomorrow

    i finished the rest of hte code though, you can check it out and see if you really care :o
     

Share This Page