0

Here is my complete working code. It successfully selects random strings from each of the first two variable arrays, but I need each of these random selections to correspond so that when "word a" is selected, we automatically get "image a.png" as well.

Is there a way to get these "option" arrays to talk to each other? Or a way to bundle the contents from one in the other so that "word a" automatically calls "image a.png" and so on? The specific syntax required to achieve this would be most appreciated!

var wordOptions = ["a","b","c","d","e"];

var imageOptions = ["a.png","b.png","c.png","d.png","e.png"];

var selectedWord = "";
var selectedImage = ""; 
var lettersinWord = [];
var numBlanks = 0;
var blanksAndSuccesses = [];
var wrongLetters = [];

var winCount = 0;
var lossCount = 0;
var remainingGuesses = 10;

function startGame()   {
 selectedWord = wordOptions[Math.floor(Math.random() * 
wordOptions.length)];
 lettersinWord = selectedWord.split("");
 numBlanks = lettersinWord.length;

 remainingGuesses = 10;
 wrongLetters = [];
 blanksAndSuccesses = [];


 for (var i = 0; i < numBlanks; i++) {
     blanksAndSuccesses.push("_");
 }

 document.getElementById("currentword").innerHTML = 
 blanksAndSuccesses.join(" ");
 document.getElementById("guessesremaining").innerHTML = 
 remainingGuesses;
 document.getElementById("wins").innerHTML = winCount;
 document.getElementById("losses").innerHTML = lossCount;

 }


function checkLetters(letter)   {
var isletterInWord = false;

for (var i = 0; i < numBlanks; i++) {
    if (selectedWord[i] == letter)   {
        isletterInWord = true;
    }
}

if (isletterInWord) {
    for (var i = 0; i < numBlanks; i++) {
        if(selectedWord[i] == letter) {
            blanksAndSuccesses[i] = letter;
        }
    }
}

else {
wrongLetters.push(letter);
remainingGuesses--

    }
}

function roundComplete() {

document.getElementById("guessesremaining").innerHTML = 
remainingGuesses;
document.getElementById("currentword").innerHTML = 
blanksAndSuccesses.join(" ");
document.getElementById("lettersguessed").innerHTML = 
wrongLetters.join(" ");



if (lettersinWord.toString() == blanksAndSuccesses.toString()) {
    winCount++;


    selectedImage = imageOptions[Math.floor(Math.random() * 
    imageOptions.length)];

    document.getElementById("image").src = selectedImage;

    document.getElementById("you").innerHTML = "YOU!";

    var titleWins = selectedWord.toUpperCase();
    document.getElementById("titlewins").innerHTML = titleWins;       
    document.getElementById("wins").innerHTML = winCount;


    startGame();
}

else if (remainingGuesses == 0) {
    lossCount++;
    alert("You Lose!");

    document.getElementById("losses").innerHTML = lossCount;

    startGame();
    }
}

 startGame();

 document.onkeyup = function(event) {
 var letterGuessed = String.fromCharCode(event.keyCode).toLowerCase();
 checkLetters(letterGuessed);
 roundComplete();

 }
  • It is an array of objects and name property corresponds to particular object in array and not the array.What you are actually trying to achieve? – amrender singh Jun 05 '18 at 16:01
  • arrayOptions is an array. it's elements have a property of name and image. that is the reason why arrayOptions.name won't work. you will have to use arrayOptions[index].name or arrayOptions[index].image – shamanth Gowdra Shankaramurthy Jun 05 '18 at 16:02
  • We need a [mcve]! – Jonas Wilms Jun 05 '18 at 16:04
  • JavaScript doesn't work like this. You have to create new arrays with the values you want. `nameOptions = arrayOptions.name` tries to get the property `name` of the value in `arrayOptions`, which is an array. Arrays don't have a `name` property so that will return `undefined`. – Felix Kling Jun 05 '18 at 16:07
  • Thank you all for the great feedback! I am attempting to display a specific image when its corresponding name is selected at random. Right now I have code that successfully picks a name from one array at random, and an image from another array at random, but they do not correspond. Could someone show me the syntax for either getting these two arrays to talk to each other or another way to bundle all this in the same array? Most appreciated! – Colin Larkin Jun 05 '18 at 16:40

0 Answers0