最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Undefined is not an object (evaluating myArray.length) - Stack Overflow

programmeradmin4浏览0评论

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>
<h1>QuickSorter</h1>
<button onclick="quicksort()">Quick Sort It!</button>
<script src="quicksort.js"> </script>
</html>

The console in safari keeps showing me this :

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.

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>
<h1>QuickSorter</h1>
<button onclick="quicksort()">Quick Sort It!</button>
<script src="quicksort.js"> </script>
</html>

The console in safari keeps showing me this :

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.

Share Improve this question asked Nov 12, 2014 at 23:37 Joshua ReddyJoshua Reddy 431 gold badge2 silver badges4 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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.

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.

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

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

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

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

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

Don't know if this is helpful here or not but document.getElementsByClassName is not returning any style,

So while accessing elements via class name It'll give you same error what you mentioned in your question.

Below Code from my project.

document.getElementById("profile-main-div").style.display=="none".  //Working
document.getElementsByClassName("profile-main-div").style.display=="none".  //Not Working
发布评论

评论列表(0)

  1. 暂无评论