0

I wrote the "bubble sort" function to sort a list of images. I can't understand why the function returns the "Uncaught TypeError: undefined is not a function". Can anyone help me?

$j(document).ready(function() { 
    var list = $j("dt").find("a").find("img");

    bubbleSort(list, list.size());    
});


function bubbleSort(a, size)
{
    do {
        var swapped = false;
        for (var i = 0; i < size - 1; i++) {
            var img = getAlt(a, i);
            var img2 = getAlt(a, i + 1);

            if (img > img2) {
                var temp = a[i].attr('src');
                a[i].attr('src') = a[i + 1].attr('src');
                a[i + 1].attr('src') = temp;
                swapped = true;
            }
        }
    } while (swapped); // <----- line error
}

function getAlt(list, pos) {
    var img = list[pos].attr("alt");
    img = img.split(' ');
    return img[3];
}
3
  • What line causes the error? Commented Nov 3, 2014 at 15:47
  • a[i].attr('src') = a[i+1].attr('src'); and a[i+1].attr('src') = temp; are not valid instructions Commented Nov 3, 2014 at 15:50
  • 1
    JavaScript has .sort for arrays which uses binary sort. You can supply a custom order function. Like: [1, 2].sort(function (a, b) { return b - a; }); Commented Nov 3, 2014 at 15:50

2 Answers 2

3

Instead of list[pos] use list.eq(pos) since with the first one you get a raw HTML element (which has no attr function) and the second way you get a jQuery object (which has an attr function)

Also use list.length instead of list.size() since the size function has been deprecated since version 1.8

Finally as noted by M. Page

a[i].attr('src') = a[i + 1].attr('src');

should be

a.eq(i).attr('src', a.eq(i + 1).attr('src'));

and the same goes for statements like it

You should also consider altering your getAlt function in the event that the img does not have an alt attribute

function getAlt(list, pos) {
    var img = list.eq(pos).attr("alt") || "";    
    if(img) {
        img = img.split(' ');
    }
    return img[3] || "";
}

All of these changes can be found in the following fiddle http://jsfiddle.net/ejhq03qy/1/

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

3 Comments

+1 for the first paragraph, since that would cause the described error, but .size() is a supported jQuery method.
...oh nevermind. Just saw that it's deprecated. So it should indeed be changed. Sorry.
I made ​​the changes but the code generates a loop. I chose to follow another way. I put all the "alt" images in an array, and after I used ".sort () + .revert ()". Thanks for the corrections.
1
a[i].attr('src') = a[i+1].attr('src');

has to be written:

a[i].attr('src', a[i+1].attr('src'));

Same for line below.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.