У меня проблемы с завершением кода.У меня есть массив объектов, и мне нужно использовать кнопку для выполнения функций reverse()
и sort()
.Я добавляю кнопки в свой HTML, но мне нужна помощь в выполнении метода «function» в моем javascript, чтобы кнопка работала на веб-странице.Я не могу понять это.Кто-то, пожалуйста, помогите.
HTML-код:
<html>
<head>
<title>Lab 9</title>
</head>
<body>
<section id="section"></section>
<script src="Lab9.js"></script>
<button onClick="reverse()">Reverse</button>
<button onClick="sort()">Sort</button>
</body>
</html>
Javascript код:
"use strict";
let motorcycles = [
{"Color": "Red", "Type": "Honda"}, {"Color": "White", "Type": "Kawasaki"},
{"Color": "Blue", "Type": "BMW"}, {"Color": "Black", "Type": "Yamaha"},
{"Color": "Red & White", "Type": "Ducati"}
];
//executes the reverse() function
function reverse() {
return motorcycles;
}
//executes the sort() function
function sort() {
}
//Unordered List
let ul = '<ul>';
//Looping through the array
for (let index = 0; index < motorcycles.length; index++) {
let motorcycle = motorcycles[index];
let motorcycleColor = motorcycle.Color;
let motorcycleType = motorcycle.Type;
ul += `<li>${motorcycleColor} ${motorcycleType}</li>`;
console.log(motorcycleColor);
console.log(motorcycleType);
}
ul += '</ul>';
document.getElementById("section").innerHTML = ul;