I am having trouble creating a code that can sort products into alphabetical order and then count the number of items in the list within the same program. I have managed to write separate programs that achieve each of these tasks individually, but when I try to combine them together, it doesn't seem to work properly. I've attempted to tweak the code multiple times, but so far, I haven't been able to make it function as intended. Any suggestions or guidance on how to solve this issue would be greatly appreciated. Here is the current code snippet:
<!DOCTYPE html>
<html>
<body>
<p>Printer, Tablet, Router, Computer, Phone.</p>
<p>Click the button to sort the products into Alphabetical Order.</p>
<button onclick="sortFunction()">Try it</button>
<p id="demo"></p>
<script>
var products = ["Printer", "Tablet", "Router", "Computer","Phone"];
document.getElementById("demo").innerHTML = products;
function sortFunction() {
products.sort();
document.getElementById("demo").innerHTML = products;
}
<button onclick="countFunction()">Count the Number of Products!</button>
<p id="demo2"></p>
<script>
var products = ["Printer", "Tablet", "Router", "Computer","Phone"];
var count=products.length;
function countFunction() {
document.getElementById("demo2").innerHTML = count;
}
</script>
</body>
</html>