0

I have 16 parts that make up whole picture shuffled on doc ready and now I must validate with an alert when whole picture is discovered. Also parts of picture shouldn't be available to switch again. I have tried this code:

fiddle: http://jsfiddle.net/w53Ls/4/

$(function() {
        $("#puzzle").sortable({
            update: function() {
                var CorrectAnswers = ["bucata0", "bucata1", "bucata2", "bucata3", "bucata4", "bucata5", "bucata6", "bucata7", "bucata8", "bucata9", "bucata10", "bucata11", "bucata12", "bucata13", "bucata14", "bucata15"];
                var UserAnswers = [];
                $('#puzzle img').each(function(){
                    var imageId = $(this).attr("id");
                    UserAnswers += imageId.replace("recordArr_", "")+",";
                });           
                    UserAnswers = UserAnswers.substr(0,(UserAnswers.length) -1);
                if(UserAnswers == CorrectAnswers){
                    alert("You  Won");
                }
            }
        });
    });
Cœur
  • 37,241
  • 25
  • 195
  • 267
user3459377
  • 241
  • 1
  • 2
  • 9

1 Answers1

0

It turns out that UserAnswers is no array (any more), while CorrectAnswers is. By simply joining the values of CorrectAnswers, the code will work

if(UserAnswers== CorrectAnswers.join(',')){
    alert("You  Won");
}

Or you rewrite CorrectAnswers to a comma separated string.

Walter Brand
  • 679
  • 3
  • 9
  • Now i see,id's of images will be changed when user start to change images with each other :( so this shouldnt happen... – user3459377 Mar 28 '14 at 14:24
  • Be careful how you want to compare the two arrays. using == will not work. See also http://stackoverflow.com/questions/7837456/comparing-two-arrays-in-javascript – Walter Brand Mar 28 '14 at 14:38
  • Good to know,but now i face with another problem...when i change two pieces id's arent changed :( – user3459377 Mar 28 '14 at 14:41