Как печатать N нет. Элементов массива каждый раз по щелчку мыши
<pre>I have an array of 20 elements. I want to display first five elements on click of a button, then next five elements on another click, and so on. I would be grateful if you could help me with the code.
I know my approach is wrong, but what I have tried displays the result two times correctly but not the third time.
Что я уже пробовал:
<body> <button onclick="nextElems();"> Try </button> <div id="yes"> </div> </body> var words = ["day", "white", "go", "low", "wise", "up", "sit", "now", "good", "grapes", "banana", "mango", "orange", "pears", "melon", "guava", "sunflower", "marigold", "jasmine", "sunflower"]; var x = ""; var count = 0; function nextElems() { if (count < words.length) { var newArray = words.slice(0, 5); x += '<div>' + newArray + '</div>'; document.getElementById('yes').innerHTML = x; count = newArray; } else { count = 0; var secondArray = words.slice(5, 10); x += '<div>' + secondArray + '</div>'; document.getElementById('yes').innerHTML = x; } x = ""; }