T.ROS T.ROS - 9 days ago 5
CSS Question

I can't get my array to print alphabetically

I'm new to Javascript.
I'm trying to get my array to print alphabetically but all the ways i have tried don't seem to work. eg array.sort().
Note that console.log doesn't print either for some reason unknown to me.
Here is my code,

<!DOCTYPE html>
<html>
<body>
<p id="products"></p>
<script>
var products = ["Printer","Tablet","Router","Monitor","Keyboard","Mouse","Webcam","Hard Drive"];
document.getElementById("products").innerHTML = products;
array.sort("products");
</script>
</body>




Many Thanks
Ted

Answer

You need to call .sort() on your array in your case its the variable products. Array Sort

var products = ["Printer","Tablet","Router","Monitor","Keyboard","Mouse","Webcam","Hard Drive"];

products.sort(); // add this line

document.getElementById("products").innerHTML = products; 
<p id="products"></p>