Implement Bubble Sort: Cannot read property 'length' of undefined

My test is failing “Cannot read property ‘length’ of undefined”…

If I print to console I can see my loop is exiting early.

function bubbleSort(array) {

    swapped = false;
    let end = array.length - 1;
    for(var i = 0; i < end; i++){
    if(array[i] > array[i + 1]){
      swapped = true;
      let temp = array[i];
      array[i] = array[i + 1];
      array[i + 1] = temp;
  }while (swapped);

“Cannot read property ‘length’ of undefined”

This is a test error log coming from the fact that your function is returning undefined. There is a test trying to access the length property of undefined – your return value – to verify your result.

"Why does my function return undefined?"

Because that’s the default return value of JavaScript functions that aren’t a constructor.

To return a value other than the default, a function must have a return statement that specifies the value to return. A function without a return statement will return a default value. In the case of a constructor called with the new keyword, the default value is the value of its this parameter. For all other functions, the default return value is undefined .

- MDN > Functions

So your bubbleSort while loop is working alright, but your function is not returning the sorted array.

Thank you. I probably should have just walked away and looked at it again in a couple hours. Javascript is really not my forte.