Problem in converting indexOf to a for loop

Tell us what’s happening:
i was revisiting the codes from freeCodeCamp which were saved in my PC, i am not able to remember which challenge was this, but i remember the question

Here is the question

`Q) take 2 arrays and pass it to a function, then check if the square of the array1 is contained in the array2 (Note: Order doesn’t matter).

`

Your code so far

function same2(arr1, arr2){
  if (arr1.length !== arr2.length){
    return false;
  }
  for (let i = 0; i < arr1.length; i++){
    let currentIndex = arr2.indexOf(arr1[i] ** 2);
    // if the square of arr1 is contained in any of the index in arr2
      if (currentIndex === -1){
        return false;
      }
        console.log(arr2);
        arr2.splice(currentIndex, 1);
    }
  return true;
  }


same2([10,2, 3, 5], [100, 4, 25, 9]);

Till now i am able to come up with this solution which i wrong ig

function same(arr1, arr2){
    if (arr1.length !== arr2.length){
        return false;
    }
    for (let i = 0; i < arr1.length; i++){
        for (let j = 0; j < arr2.length; j++){
            let currentIndex = arr1[i] ** 2;
            console.log(currentIndex);
            if (currentIndex === -1){
                return false;
            }
            if (arr2[j] === currentIndex){
                arr2.splice(currentIndex, 1);
//                 console.log(arr2);
            } 

        }
    }
    return true; 
}
same([1, 10,2, 4], [10, 1, 16, 4]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36.

Does it have a challenge if so mind leaving the link to it?

i was revisiting the codes from freeCodeCamp which were saved in my PC, i am not able to remember which challenge was this, but i remember the question

Q) take 2 arrays and pass it to a function, then check if the square of the array1 is contained in the array2 (Note: Order doesn't matter).

Not familiar with such a challenge. Do you have an example of a test case and the expected return value of the function? I am not exactly sure what “check if the square of the array1 is contained in the array2” even means.

its like the arr1 has some elements [1,2,2] and we have to check in the arr2[1,4,4] if there are sq of the elements of arr1 contained in it.

console.log(same([1,2,2],[1,4,4]));  // output: true
console.log(same([1,2,2],[1,1,4]))   // Output: false

Originally, you said order does not matter, so why would the 2nd test case not be true, since at least one of the values in the second array is the square of one of the values in the first array? This is not a currently Free Code Camp challenge. If it was a challenge at one point, then it must have been a couple years back, because I can not find it in the recent history. Being able to see the exact working of the challenge and all the test cases would definitely clear things up.

for the case that order doesn’t matter it meant

console.log([1, 10, 2, 5, 3], [100, 9, 25, 4, 1]);

for this case

console.log(same([1,2,2],[1,4,4]));  // output: true
console.log(same([1,2,2],[1,1,4]))   // Output: false

if there are 2 duplicates in arr1[1,2,2] then there should be 2 duplicates in arr2[1,4,4] as well. That is why it is returning true, but in the other case when we have arr2[1,1,4] there is no sq. for arr1[1] index so it returns false.

So console.log(same([1,2,2],[4,1,4])) should return true?

Will the length array1 and array2 always be equal in each test case? For example, if array1 has 4 elements, then array2 will have 4 elements. If array1 has 2 elements, then array2 will have 2 elements?

yes it should in the this case

@mehul1011 Did you see my second question above?

yes it should be of equal length, if its not then its should return false.

for example

if (arr1.length !== arr2.length)
{
return false;
}
else {
// we execute our code to check for same elements (one sq of another) in both arrays
}

If that is the case, I would recommend the following solution. It would be much more efficient than the nested for loops or using indexOf within a for loop.

const same = (arr1, arr2) =>{
  const arr1SumOfSquares = arr1.reduce((sum, val) => sum + val * val, 0);
  const arr2Sum = arr2.reduce((sum, val) => sum + val);
  return arr1SumOfSquares === arr2Sum;
};

but the thing is that i am not to break the code in case of 2 for loops your approach and code is correct but breaking that code with 2 for loop has more important than finding another solution.

this is an issue you have in your code - you calculate the square but then use it as an index, and in many cases you don’t have that many items in the array

when you use the two loops remember that the indexes are i and j

if you can provide even an hint that would be good, like how to replace that bug (currentIndex) in the code.

look what’s currentIndex in the other version of the code - you use them in the same way, but are they obtained in the same way?

when you use two loops you don’t want to use indexOf, right? when you use two loops the indexes are i and j, you don’t need currentIndex

when you use two loops, the inner loop doesn’t need t keep looping through the whole array if it has already found the square - you can use break to stop the loop to continue looping

in your code the approach you are taking is that, you sq, add all the elements in the arr1 and store in arr1SumOfSquares and sum all the elements in arr2 and store in arr2Sum and then compare both arr1SumOfSquares and arr2Sum, then if they are equal then return true otherwise false.

you could also do this with two for loops

yeah, first i used for loop and one indexOf and i want to replace that indexOf with a for loop, i will try using break and avoid currentIndex.

yeah that’s what i want to do but i have error with my code, i will go through it again once.