0

I'm trying to do a check that the first array contains the same values of the second array. However I'm confused about my code.

First question is: why is my code running my else statement if all letters in the first array are contained in the second? it will run 2 lines of "this is not valid"

Second question is: if my first array contains a duplicate letter it will still pass the check e.g ["a", "b" , "a", "d", "e", "f"]; even though there is two a's in the first it will see the same "a" again. Anyone know a way around this.

Sorry for my long winded questions but I hope it makes sense. Thanks :)

var letters = ["a", "b" , "c", "d", "e", "f"];
var otherLetters = ["a","b", "c" , "d", "e", "f"];

var i = -1;

while(i<=letters.length){



i++;
    if(otherLetters.includes(letters[i])){
        console.log("This is valid");
    }

    else 

    console.log("This is not valid");



}

0

7 Answers 7

1

You didn't close the brackets. And your loop is very confusing, please use foreach. Here is a working example:

const letters = ["a", "b" , "c", "d", "e", "f"];
const otherLetters = ["a","b", "c" , "d", "e", "f"];

letters.forEach(el => {
  if (otherLetters.includes(el)) {
    console.log(el + 'is valid');
  } else {
    console.log(el + 'is not valid');
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Nico, I feel quite silly now haha. Thanks for your answer was able to fix up my code :)
1

You are trying to access array elements which are out of bounds. The script runs 8 iterations over an array with 6 elements.

1 Comment

Thanks Matthias, got it sorted now :)
0

Nothing to worry, cpog90.

Try this solution.

    var letters = ["a", "b" , "c", "d", "e", "f"];
    var otherLetters = ["a","b", "c" , "d", "e", "f"];
    
    var i = 0;
    while(i<letters.length){
        if(otherLetters.includes(letters[i])){
            console.log("This is valid");
        }
        else {
           console.log("This is not valid "+i);
        }
        i++;
    }

What went wrong in your logic?

If you declare i = -1 and while(i<=letters.length), As 6 is length of letters, 8 iterations will be done as follows.

For first iteration (i = -1), 'while' condition returns true and checks for 'a'

output: This is valid

For second iteration (i = 0), 'while' condition returns true and checks for 'b'

output: This is valid

For third iteration (i = 1), 'while' condition returns true and checks for 'c'

output: This is valid

For fourth iteration (i = 2), 'while' condition returns true and checks for 'd'

output: This is valid

For fifth iteration (i = 3), 'while' condition returns true and checks for 'e'

output: This is valid

For sixth iteration (i = 4), 'while' condition returns true and checks for 'f'

output: This is valid

For seventh iteration (i = 5), 'while' condition returns true and checks for undefined value.

output: This is not valid

For eighth iteration (i = 6), 'while' condition returns true and checks for undefined value.

output: This is not valid

1 Comment

Thanks for the clear explanation What If! Again I dunno why I thought I would start from -1 lol. Was able to fix my code up :)
0

First of all you have set i = -1 which is confusing since array start position is 0. The reason your loop is running two extra times is because loop started at -1 instead of 0 and next the condition i <= length.

Since [array length = last index + 1] your loop runs extra two times. Just to make your code work assign var i = 0 and while condition i < letters.length

Comments

0

Simplest solution is using lodash. It has all optimizations out-of-the-box:

var letters = ["a", "b" , "c", "d", "e", "f"];
var otherLetters = ["f", "a","b", "c" , "d", "e"];

const finalLetters = _.sortBy(letters);
const finalOtherLetters = _.sortBy(otherLetters);

if (_.isEqual(finalLetters, finalOtherLetters)) {
  console.log('Two arrays are equal.');
} else {
  console.log('Two arrays are not equal.');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Comments

0

Arrays are index based and starts from 0. So, the -1 and the less than letters.length check puts the code into out of bounds.

var letters = ["a", "b" , "c", "d", "e", "f"];
var otherLetters = ["a","b", "c" , "d", "e", "f"];
var i = 0;
while(i<letters.length)
{
    if(otherLetters.includes(letters[i]))
    {
        console.log("This is valid");
    }
    else
    {
        console.log("This is not valid");
    }
    i++;
}

1 Comment

Thanks Abhishek! I have no idea why i decided to start from -1 tbh. I think my brain is fried lol.
0

You can use a combination of Array.prototype.every with Array.prototype.includes, along with some extra guard clauses.

const areSequenceEqual = (arr1, arr2) => {
  if (!arr1 || !arr2) {
    return false;
  }

  if (arr1.length !== arr2.length) {
    return false;
  }

  return arr1.every(x => arr2.includes(x));
};

const letters = ["a", "b", "c", "d", "e", "f"];
const otherLetters = ["a", "b", "c", "d", "e", "f"];
const someOtherLetters = ["a", "b", "c", "d", "e", "f", "g"];

console.log(areSequenceEqual(letters, otherLetters));
console.log(areSequenceEqual(letters, undefined));
console.log(areSequenceEqual(letters, someOtherLetters));

1 Comment

Thanks Kunal, I was able to make use of the include method to resolve my issue :)

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.