+ 1

Can I solve this in just a single "for" loop. Or is it impossible?

https://code.sololearn.com/ceFVnFidR1ic/?ref=app I have 2 arrays with same length. Each squared number of first array should have corresponding number in second array and vise versa. i.e. let arr1 = [2,4]; let arr2 = [4,16] // or [2*2, 4*4]; So I treat them equal. Elements in arrays can be in random order as in code. I described my problem in comments of code.

28th Aug 2020, 1:52 AM
Artur
Artur - avatar
12 Answers
+ 5
Artur Here's my solution in case you get stuck. https://code.sololearn.com/ctgeIWy0gP4l/?ref=app
29th Aug 2020, 6:33 AM
David Carroll
David Carroll - avatar
+ 4
Artur Assuming recursion was also not allowed and efficiency wasn't important, then I suppose you could accomplish this within a single infinite looping construct where the break condition occurred within the loop block. This single loop would need separate variables to store the current index (i and j) for each respective array with reset logic to be applied to the 2nd array after reaching the array length. The first array index would likely only increment when the 2nd array index was equal to the length of the 2nd array. You could even attempt to count the items in the array this way. Consider this super simple code: let a = [2, 4] let b = [4, 16] let i = 0 let j = 0 let i_size = null let j_size = null for( ; ; ) { // same as while(true) //Count length if( null == i_size ) { if("undefined" == typeof a[i] ) { i_size = i - 1//set length i = 0 //reset i } else { i++ //increment i } contiue //Skip everything else below //until i_size has a value. } }
28th Aug 2020, 6:00 AM
David Carroll
David Carroll - avatar
+ 3
You can iterate <array2>, and find a square root of each number from <array2> inside <array1> using array `indexOf` method. When the `indexOf` method returns -1, you'll know that number from <array2> is no square of either number in <array1>. for(let n of array2) { let needle = Math.sqrt(n); let pos = array1.indexOf(needle); console.log(`square root of ${n} -> ${needle} found at ${pos}`); }
28th Aug 2020, 2:29 AM
Ipang
+ 2
Good idea! But.. there is still array method indexOf(). What I wanted is to do that without any array methods (I know it sounds stupid tho šŸ¤£). With array methods there are a few other ways to solve this easily, i.e.: array2.every(i => array1.some(j => j * j == i)); Or we could filter() same numbers in both arrays and problem departs.
28th Aug 2020, 3:02 AM
Artur
Artur - avatar
+ 2
Okay, I thought you only don't want to use every() or some(). It will be pretty hard to do with a single loop as your title described though šŸ˜
28th Aug 2020, 3:06 AM
Ipang
+ 2
Agree. Might be even impossible to do that w/o methods not just hard. I was just wondered if it's possible.
28th Aug 2020, 3:15 AM
Artur
Artur - avatar
+ 2
To add more complexity, you can add "avoid use of length property" to the requirements šŸ˜‚
28th Aug 2020, 3:18 AM
Ipang
+ 2
That would be the mightiest kidding in the world
28th Aug 2020, 3:20 AM
Artur
Artur - avatar
+ 2
I just got inspired by a question in Q&A recently, no hard feelings man šŸ˜Š
28th Aug 2020, 3:22 AM
Ipang
+ 2
This will keep growing, but hopefully you get the idea.
28th Aug 2020, 6:01 AM
David Carroll
David Carroll - avatar
+ 2
Wow what an amazing idea. I'll definitely try to implement it. Thank you David Carroll !!
28th Aug 2020, 9:00 AM
Artur
Artur - avatar
+ 1
And please don't suggest me to use any of methods like every() or some() etc. I really want to understand how to do this with just "for" loops.
28th Aug 2020, 2:01 AM
Artur
Artur - avatar