Why are the functions wrongly comparing the masses?



  • I have the following function to compare the masses:

    a = [1,'a']
    b = [1,'b']
    

    function compareArrays(arr1, arr2) {
    let c = 0;
    if (arr1.length == arr2.length) {
    for(let i = 0; i <= arr1.length; i++) {
    if (arr1[i] === arr2[i]) {
    c++;
    console.log(c)
    }
    }

        if (c === arr1.length) {
                return true;
        }
    } return false;
    

    }

    console.log(compareArrays(a, b))

    I expect to get false, because the second elements of the masses don't match, but they get true. Why and how to fix it?



  • Subject problem i <= arr1.length♪ As a result, the cycle is carried out 3 times, not 2. And we get the dope. if (undefined === undefined ) { c++

    To find such mistakes, learn. https://ru.stackoverflow.com/a/701140/191482



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2