I have written this code but it isn't working . It's displaying the unsorted array as well as the button but when I click on the button nothing happens. I am new to javascript. What I know so far is that we can call functions using the onclick method by javascript. We can write functions as we write in c or c++ . That's what I think I have done here but it isn't showing the sorted array .
var myarray = [4, 6, 2, 1, 9, ];
document.getElementById("demo").innerHTML = myarray;
function sort(myarray) {
var count = array.length - 1,
swap,
j,
i;
for (j = 0; j < count; j++) {
for (i = 0; i < count; i++) {
if (array[i] > myarray[i + 1]) {
swap = myarray[i + 1];
myarray[i + 1] = myarray[i];
myarray[i] = swap;
}
}
document.write(myarray);
}
}
<p>Click the button to sort the array.</p>
<button onclick="sort()">Try it</button>
<p id="demo"></p>
array is not defined