8

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>

3
  • 1
    @mmm its attached to button click. Also @Nancy, you should check your console. there is an error array is not defined Commented Mar 27, 2016 at 11:07
  • Check you function, then check console, there are obvious errors in your code (hint: array vs myarray). And stop using document.write. Commented Mar 27, 2016 at 11:10
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Mar 27, 2016 at 11:11

5 Answers 5

3

The main problem is, to decide if a parameter should be set or not. If not, then use myarray for all operations. If you decide to use a parameter, then use only the parameter variable.

var myarray = [4, 6, 2, 1, 9, ];
document.getElementById("demo").innerHTML = myarray;

function sort() {                                        // no need for a parameter
    var count = myarray.length - 1,                      // change it to myarray
        swap,
        j, i;

    for (j = 0; j < count; j++) {
        for (i = 0; i < count; i++) {
            if (myarray[i] > myarray[i + 1]) {           // access only myarray
                swap = myarray[i + 1];
                myarray[i + 1] = myarray[i];
                myarray[i] = swap;
            }
        }
    }
    document.getElementById("demo").innerHTML = myarray; // return the sorted array
}
<p>Click the button to sort the array.</p>
<button onclick="sort()">Try it</button>
<p id="demo"></p>

Sign up to request clarification or add additional context in comments.

Comments

2

var myarray = [4, 6, 2, 1, 9];
document.getElementById("demo").innerHTML = myarray;

function sort(myarray) {
  var count = myarray.length - 1,
    swap,
    j,
    i;

  for (j = 0; j < count; j++) {
    for (i = 0; i < count; i++) {
      if (myarray[i] > myarray[i + 1]) {
        swap = myarray[i + 1];
        myarray[i + 1] = myarray[i];
        myarray[i] = swap;
      }
    }       
  }
 document.getElementById("result").innerHTML = myarray;
}
<p>Click the button to sort the array.</p>
<button onclick="sort(myarray)">Try it</button>
<p id="demo"></p>
<p id="result"></p>

Comments

1

I found 2 problems with your code. The first is that you wrote array instead of myarray in 2 lines:

var count = array.length - 1,

and

if (array[i] > myarray[i + 1]) {

The second problem is that you didn't pass your array as argument, when calling the function, so:

<button onclick="sort()">Try it</button>

should be:

<button onclick="sort(myarray)">Try it</button>

Comments

1

Firstly you array definition has an extra , at the end. Next, you have made use of array and not myarray in the if statement.

Once you sort, you need to have a statement:

document.getElementById("demo").innerHTML = myarray;

1 Comment

Its fine to use arrays with extra ,. this does not gives any errors.
1

You are using a variable 'array' which is not defined or known to the function. Moreover on buttonclick you are not passing any parameter to sort function. That's the reason you're getting an error.

var myarray = [4, 6, 2, 1, 9, ];

function sort(myarray) {
  var count = myarray.length - 1,
    swap,
    j,
    i;

  for (j = 0; j < count; j++) {
    for (i = 0; i < count; i++) {
      if (myarray[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(myarray)">Try it</button>
<p id="demo"></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.