I am trying to create a loop through an array (array) and display the elements one by one only after clicking a button (bt). However, when I run this code, it only shows the last element of the array (i.e. honda). Can someone please help me fix this issue?
var hints = document.querySelector(".hint");
var array = ["Car", "bmw", "mercy", "porsche", "hyundai", "jeep", "honda"];
var bt = document.querySelector("button");
for (var i = 1; i < 6; i++){
bt.addEventListener("click", function(){
hints.textContent = array[i];
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Password</title>
<link rel="stylesheet" href="password.css" type="text/css">
</head>
<body>
<h1 class="hint"></h1>
<button type="button" name="button">Click me</button>
<script src="password.js" charset="utf-8" type="text/javascript"></script>
</body>
</html>