2

Only just started learning JavaScript.

Why won't my button display the values in my "authors" array?

/* JavaScript file */
var authors = ['Ernest Hemingway', 'Charlotte Bronte',
  'Dante Alighieri', 'Emily Dickinson'
];

function ShowAuthors(authors) {
  for (i = 0; i <= authors.length; i++) {
    document.write(authors[i]);
  }
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <script src="./js/script.js"></script>
  <link rel="stylesheet" href="./css/style.css" />
</head>

<body>
  <div class="container">
    <input type="button" class="author" value="Show Authors" onClick="ShowAuthors()" />
  </div><br>
</body>

</html>

Sorry for the terrible formatting, I'm new here. I'm probably missing something super basic..

3
  • 4
    Because you don't pass any list of authors in onClick. Commented Jan 11, 2018 at 17:31
  • Prerak Sola above is right. One way of solving this would be to NOT have a parameter in your function and just use the global variable. The other way is to leave the function as is but then you will need to pass some value to your function when calling it. Commented Jan 11, 2018 at 17:34
  • You should read about scope of variables and shadowing Commented Jan 11, 2018 at 17:37

1 Answer 1

1

As someone else already said, you weren't passing in authors, and since you had a parameter named authors it wouldn't use authors defined in your js file.

Also, when you iterate through your loop, you should not do <= to length, as arrays are zero index.

Finally, your code would be formatted more cleanly if you use authors.join(', ') to write instead of looping through the values without adding any additional formatting.

/* JavaScript file */
var authors = ['Ernest Hemingway', 'Charlotte Bronte',
  'Dante Alighieri', 'Emily Dickinson'
];

function ShowAuthors() {
  document.write(authors.join(', '))
}
<!DOCTYPE html>
<html>

<head>
  <title></title>
  <script src="./js/script.js"></script>
  <link rel="stylesheet" href="./css/style.css" />
</head>

<body>
  <div class="container">
    <input type="button" class="author" value="Show Authors" onClick="ShowAuthors()" />
  </div><br>
</body>

</html>

Sign up to request clarification or add additional context in comments.

5 Comments

I didn't feel right about the parameter of authors but wasn't sure if the var was global. I guess I need to do more research on variables. Thank you for the suggestions. I will look into all of the suggestions.
I would recommend looking into scope of variables. It can be unintuitive in some cases. If this answer answered your question then please accept as an accepted answer.
The root of the problem was not only the parameter I had listed, but also the ".length()" should have been ".length". Now working properly, albeit possibly not optimally. Learning has occurred.
Also with length, remember that arrays are 0 indexed, so <= array.length will run one too many times in most cases.
Yes! That was my first edit. I learned that, it just isn't habit yet. Thank you much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.