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.
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).
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.
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
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.