Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I keep getting array not defined as an error when I am trying to push guesses into an array

I am trying to push the correct guess into an array and print out how many guesses it took (in this case one, cause I’m just inputting the correct one) and then printing out what that guess was, but I am continually getting array not defined.

var guesses = [];

function do_guess() {
  let guess = Number(document.getElementById("guess").value);
  let message = document.getElementById("message");
  if (guess == num) {
    guesses.push(guess)
    numguesses = array.length(guesses)
    message.innerHTML = "You got it! It took you " + numguesses + " tries and your guesses were " + guesses;
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The correct way of getting the number of elements inside an array in js :

arrayInstance.length

In your case it would be

 guesses.length

Read more here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading