438,430 views
18 votes
18 votes
Declare a variable using var named hobbies and set it equal to an array with three strings inside of it. Use window.alert to show the contents of hobbies. Create a new variable myHobby and set it equal to the first element in the array. Set the innerHTML of the the HTML element with the id of hx to the myHobby variable. Now, set the innerHTML of the HTML element with the id of hz to the the third element in the hobbies array using bracket notation to access the element. Do this on a single line, not create a variable for either the li element or the array value before you write it. Awesome, you can access each element in an array using the index. But what happens if you try to access an index that is beyond the last element

User Ashu Kumar
by
2.6k points

1 Answer

22 votes
22 votes

Answer:

Hard to read this so im just going to write as I read.

Step-by-step explanation:

// Create array

let hobbies = ["string 1", "string 2", "string 3"];

// Pop an alert for each hobby

for (let i = 0; i < hobbies.length; i++) {

window.alert(hobbies[i]);

}

// Or to show all at once

windows.alert(hobbies[0] + "\\" + hobbies[1] + "\\" + hobbies[2]);

// Creating myHobby variable

let myHobby = hobbies[0];

// Changing 'hx' elements innerHTML to the 'myHobby' variable

document.getElementByID("hx").innerHTML = myHobby;

// Changing the 'hz' element to the third index of the 'hobbies' array.

// Array index's start at 0, so 1 2 3 is equal to 0 1 2

document.getElementByID("hz").innerHTML = hobbies[2];

// if you referenced hobbies[2]; it would throw an error saying you are accessing a non-existent index. Not those exact words but something like that, lemme know if you need anymore help. This is for JavaScript BTW and it is not tested so there could be Errors

User Jsnow
by
2.1k points