未定义的不是对象(计算myArray.length)

时间:2021-06-02 11:44:24

I've been working on a program that uses a quick sort algorithm to sort an array of numbers. This is my code:

我一直在研究一个程序,它使用快速排序算法来对数字数组进行排序。这是我的代码:

    var myArray=[8,2,5,6];

function quickSort(myArray)
{
    if (myArray.length === 0){
        return [];
    }

    var left=[];
    var right=[];
    var pivot= myArray[0];

    for (var i=1; i<myArray.length; i++){

        if (myArray[i]<pivot) {
            left.push(myArray[i]);

        }

        else {

            right.push(myArray[i]);
        }
    }

    return quickSort(left).concat(pivot, quickSort(right));
    document.getElementById().innerHTML = quickSort(left).concat(pivot, quickSort(right));
}

And here is my html

这是我的html

<html>
<h1>QuickSorter</h1>
<button onclick="quicksort()">Quick Sort It!</button>
<script src="quicksort.js"> </script>
</html>

The console in safari keeps showing me this :

safari中的控制台不断向我展示:

TypeError: undefined is not an object (evaluating myArray.length)

I really don't understand at all why it isn't working. Any help would be appreciated.

我真的一点也不明白为什么它不管用。如有任何帮助,我们将不胜感激。

2 个解决方案

#1


6  

If you are calling the function with

如果你调用这个函数。

quicksort()

you are not passing any arguments in. However, your function starts with

你没有传入任何参数。但是,函数从

function quickSort(myArray)
{
  if (myArray.length === 0){
    return [];
  }

Since no arguments are passed to quicksort, myArray is left undefined. Undefined variables are not objects, and therefore can not have properties such as length.

由于没有将参数传递给quicksort,所以myArray没有定义。未定义的变量不是对象,因此不能具有诸如长度之类的属性。

EDIT:

编辑:

In order to call your function when you click the button, it is best to set the event listener in javascript rather than putting it in your HTML.

为了在单击按钮时调用函数,最好使用javascript设置事件监听器,而不是将其放在HTML中。

Give you button an ID name and remove the onclick attribute:

给你的按钮一个ID名,并删除onclick属性:

<button id="sortButton">Quick Sort It!</button>

Then add the event listener in your JavaScript after your quickSort definition:

然后在快速排序定义之后在JavaScript中添加事件监听器:

document.getElementById("sortButton").onclick = function(){quickSort(myArray);});

#2


3  

You dont pass the parameter to the function in your html onclick. So myArray ends up being undefined inside your func. Notice that myArray that u defined outside the function is not the same myArray inside of it defined as a parameter tp the function

您不会将参数传递给html中的函数onclick。myArray在你的func中没有定义。注意,u在函数外定义的myArray与函数中定义的参数tp不同

#1


6  

If you are calling the function with

如果你调用这个函数。

quicksort()

you are not passing any arguments in. However, your function starts with

你没有传入任何参数。但是,函数从

function quickSort(myArray)
{
  if (myArray.length === 0){
    return [];
  }

Since no arguments are passed to quicksort, myArray is left undefined. Undefined variables are not objects, and therefore can not have properties such as length.

由于没有将参数传递给quicksort,所以myArray没有定义。未定义的变量不是对象,因此不能具有诸如长度之类的属性。

EDIT:

编辑:

In order to call your function when you click the button, it is best to set the event listener in javascript rather than putting it in your HTML.

为了在单击按钮时调用函数,最好使用javascript设置事件监听器,而不是将其放在HTML中。

Give you button an ID name and remove the onclick attribute:

给你的按钮一个ID名,并删除onclick属性:

<button id="sortButton">Quick Sort It!</button>

Then add the event listener in your JavaScript after your quickSort definition:

然后在快速排序定义之后在JavaScript中添加事件监听器:

document.getElementById("sortButton").onclick = function(){quickSort(myArray);});

#2


3  

You dont pass the parameter to the function in your html onclick. So myArray ends up being undefined inside your func. Notice that myArray that u defined outside the function is not the same myArray inside of it defined as a parameter tp the function

您不会将参数传递给html中的函数onclick。myArray在你的func中没有定义。注意,u在函数外定义的myArray与函数中定义的参数tp不同