Basic displaying array values?

lvboogart

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..

user3483203

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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Displaying values from an array in Twig

Functions in array not displaying the correct values?

Displaying values of Array. Getting strange values

Obtaining $_SESSION array values and displaying using echo

Why is my parallel array displaying random values?

Map an array of objects but only displaying unique values

Displaying some values from an array VBA excel

Displaying the values of an n-dimension array as a string

Multiplying and displaying the values of an array in C++

Displaying array values in element grouped by key

Displaying array values through a submit input in PHP

displaying values stored in input array in html page

Storing text box input into an array and displaying it on to a list box in visual basic

JSON values not displaying when outputting multiple values (array) to HTML

Displaying values above certain number in array and counting how many values there are

Add values from basic array to Set<String>

PHP array displaying last row values from table

Displaying array values based on a certain condition in a blade - Laravel

Angular - displaying text based on comparing with array values in template

Second TableView not displaying any/correct nest array values

Multi-dimensional PHP array of values not displaying correctly

Why array is displaying 'undefined' for the later values to be inserted in next indexes?

Sort array of objects by key values and displaying them on a HTML element

Java Array split() displaying all values but giving arrayindexoutofbound exception error

SwiftUI problem with displaying values from array using MVVM pattern

JTable displaying values wrong/not displaying whole values

I am displaying the values of a multidimensional array. The values are being displayed but i am getting a notice of Undefined offset

How to replacing all missing values in numpy array with 0 and displaying the last 15 values of the attribute

Code Help - Used scanner to initialize string array. When displaying, array values are all blank