user10209332
user10209332

Reputation:

I am having problems appending a value into an array

var secretWord = [];
var underScoreWord = [];
// var guesses = [];
var wordLetter = false;

var city = ["Paris", "Wellington", "Hanoi", "Perth", "Marseille", "London", "Ottawa", "Zurich", "Boston", "Tokyo", "Detroit"];

// console.log(city);

// Pick random word from the team array and push the result to an empty array. 
// FUNCTION 1 pick random city
function pickRandomCity() {
    var randomCity = city[Math.floor(Math.random() * city.length)];
    secretWord.push(randomCity);
    return randomCity;
}

var cityPicked = pickRandomCity();

// Get length of secretWord and push as underscores to am empty array
for (var i = 0; i < cityPicked.length; i++) {
underScoreWord.push("_");
}

console.log(secretWord);
console.log(underScoreWord);



// Check for letters
//listen for key press and check to see if its a match
document.onkeyup = function letterCheck(event) {
    var userGuess = event.key;  
  
    for (var j = 0; j < cityPicked.length; j++) {
        if (userGuess === cityPicked[j]) {
            wordLetter = true;
        }
        if (wordLetter) {
        underScoreWord.push(userGuess);
        }
    }



    console.log(wordLetter);
    

}

Inside the onkeyup function i am trying to push the result (the key pressed) into theunderScoreWord array. It is converting the wordLetter boolean to true when i type the correct key but i cant figure out how to push it to display inside the word so it shows like this _ _ N _ _ _

I think i am close but then again i could be miles off. Any tips?

Upvotes: 2

Views: 68

Answers (3)

saAction
saAction

Reputation: 2075

You want to fill _ value by key pressed with matching word press in underScoreWord array

Example :

  • if Tokyo is selected as rendom city,

  • it will store [ "_", "_", "_", "_" ] in underScoreWord

  • Now if use press T it will fill inside [ "T", "_", "_", "_" ]

  • After that use pressed o it will fill inside [ "T", "o", "_", "o" ]

Problem :

  • secretWord is array but storing entire word in 1 length
  • fill data in underScoreWord only the same index of the correct spelling match

Please check below solution :

var secretWord = [];
var underScoreWord = [];
var guesses = [];
var wordLetter = false;

var city = ["Paris", "Wellington", "Hanoi", "Perth", "Marseille", "London", "Ottawa", "Zurich", "Boston", "Tokyo", "Detroit"];

// Pick random word from the team array and push the result to an empty array. 
// FUNCTION 1 pick random city
function pickRandomCity() {
    var randomCity = city[Math.floor(Math.random() * city.length)];
    secretWord  = randomCity.split('');
            
    return randomCity;
}

var cityPicked = pickRandomCity();

// Get length of secretWord and push as underscores to am empty array
for (var i = 0; i < cityPicked.length; i++) {
    underScoreWord.push("_");
}

console.log('secretWord : ' + secretWord);
console.log('underScoreWord : ' + underScoreWord);
console.log('------------------');
console.log('cityPicked : ' + cityPicked);



// Check for letters
//listen for key press and check to see if its a match
document.onkeyup = function letterCheck(event) {
    var userGuess = event.key;
    for (var j = 0; j < secretWord.length; j++) {
        if (userGuess === secretWord[j]) {
            wordLetter = true;
            underScoreWord[j]= userGuess;
        }
    }
    console.log(underScoreWord);
}

Upvotes: 1

Barmar
Barmar

Reputation: 782785

You don't need to push the letter onto underScoreWord. Just replace the corresponding underscore with the letter.

for (var j = 0; j < cityPicked.length; j++) {
    if (userGuess == cityPicked[j]) {
        underScoreWord[j] = userGuess;
    }
}

Upvotes: 0

nosleepfilipe
nosleepfilipe

Reputation: 76

Try this one

Replace this

underScoreWord.push(userGuess);

For this

underScoreWord[j] = userGuess

Upvotes: 0

Related Questions