0

I got two arrays, both type string :

var correctAnswers = ["0", "1", "2", "3", "4", "5"];

and second :

var finalArray = ["0", "1", "4", "3", "5", "2"];

I would like to compare them and and if not match second with first to extract elements and push them in another array, like this :

finalArray = ["0", "1", "3"];

var toChange = ["4", "5", "2"];
9
  • Do you also want to store the index/position of the incorrect answers? Commented May 5, 2014 at 10:08
  • Nope, position doesnt need to be same. Just to push elements that doesnt match position and number with correctAnswer array into toChange array. Commented May 5, 2014 at 10:10
  • 5
    Attempt : 0% Effort : 0.1% Commented May 5, 2014 at 10:11
  • 1
    What about "3" in your example? Commented May 5, 2014 at 10:12
  • 1
    @user3459377 There is no dumb question. Just lazy questions. Commented May 5, 2014 at 10:13

4 Answers 4

1

You could use jQuery map method too:

var toChange = $.map(finalArray, function(v, k){
    return correctAnswers[k] !== v ? v: null;
});

finalArray = $.map(finalArray, function(v, k){
    return correctAnswers[k] === v ? v: null;
});

DEMO

Or using array.prototype.filter():

var toChange = finalArray.filter(function(v, k){
    return correctAnswers[k] !== v;
});

finalArray = finalArray.filter(function(v, k){
    return correctAnswers[k] === v;
});

filter DEMO

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

2 Comments

I've been running of using map, but it seems it work like a charm +1
@user3459377 this is jQuery map method. Using array.prototype.map() wouldn't work as expected here, maybe that was your issue here. Indeed, you should use filter() instead.
1
var toChange = [];
for (var i = finalArray.length - 1; i >= 0; i--) {
    if (finalArray[i] !== correctAnswers[i]) {
        toChange.push(finalArray[i]);
        finalArray.splice(i, 1);
    }
}

The key to this is iterating down from the length to 0, rather than up from 0 to the length as usual. This is because splice changes the indexes of all the elements after the element being removed, so normal iteration would skip an element after each splice.

Comments

1

This will do it:

http://jsfiddle.net/XiozZe/NkM6s/

var correctAnswers = ["0", "1", "2", "3", "4", "5"];
var finalArray = ["0", "1", "4", "3", "5", "2"];
var correctFinal = [];
var toChange = [];

for(var i = 0; i < correctAnswers.length; i++){
    if(correctAnswers[i] === finalArray[i]){
        correctFinal[correctFinal.length] = finalArray[i];
    }
    else{
       toChange[toChange.length] = finalArray[i];
    }   
}

1 Comment

Also, great answer too!! Thanks for ur time
1

First you've got to define your variables

// Define your variables
var correctAnswers = ["0", "1", "2", "3", "4", "5"];
var answers     =    ["0", "1", "4", "3", "5", "2"];
var finalArray = [];
var toChange = [];

Then you create a loop, which loops over the Answers array.

// comparing them could be done with a simple loop
for (var i = 0; i<answers.length; i++) {
    if (correctAnswers[i] == answers[i]) { //if they're equal, push the value into the final array
        finalArray.push(answers[i]);
    } else { // if not, push them into the toChange array
        toChange.push(answers[i]);
    }
}

This will give you toChange = [0, 1, 3] If you want toChange = [0, 1] you've got to change it to

toChange = answers.slice(0);

for (var i = 0; i<correctAnswers.length; i++) {
    if (correctAnswers[i] == Answers[i]) { //if they're equal, push the value into the final array and remove the first value from the toChange array
        finalArray.push(Answers[i]);
        toChange.shift();
    } else { // if not, break
        break;
    }
}

Fiddle

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.