I am trying to create a function that will allow me to click through the marvel names at random. However, I have tried to implement the following code but it only shows the one name and does not allow me to click through the different names. Help please.
var arr = data.data.results[1].name;
console.log(arr)
function nextItem() {
for (i = 0; i <arr.length; i++){
i = i + 1;
i = i % arr;
} return arr}
function prevItem () {
if (i===0) {
i = name;
}
i = i - 1;
return name[i]
}
document.getElementById('character-name').textContent = arr;
document.getElementById("next-btn").addEventListener('click', function (e){
document.getElementById("character-name").textContent= nextItem();
}
) });
I have added a picture of the MARVEL API properties and associated values of an object Marvel API Console.log