Quotes help make search much faster. Example: "Practice Makes Perfect"

Wednesday, December 19, 2012

LearnStreet Mastermind Game Sample Solution Method 3



function checkAnswerWrongPlace(ans, realanswer){
    // This method compares two input strings representing a player's guess ("ans")
    // at the correct sequence of colored pegs and the actual correct solution
    // ("realanswer"). But instead of returning the correct number of positional
    // matches between the two strings, this method returns the number of
    // character matches between the two strings where the matching characters
    // are not in the same position in both strings: hence "right answer, wrong place".
    //WRITE YOUR CODE HERE
    var numCharMatches = 0;
    for( var i=0; i < 4; i++ ){
        for( var j=0; j < 4; j++ ){
            if(ans.charAt(i)===realanswer.charAt(j) && i!=j){
                numCharMatches++;
            }
        }
    }
    return numCharMatches;
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts