Hello, first of all, I don’t have access to PC, I hope I can describe my issue well enough. I would like to ask you, how can I push value to an array only after all looped values meet the same condition. I have a nested array, the first (i) loops from 1 to a certain number and the second one (j) loops through an array. I want to save that specific (i), only if it matches the same condition for all array values of the second loop (j). For example, if I wanted to find common multiplier for all the numbers of the array. It works, when I use && bitwise operators and force all the conditions into if statement. But it is not a solution, I only checked wheter my logic is ok. When I write, for example, i % arr[i] instead of forcing all individual array values with && (i % arr[0] == 0 && [arr1] == 0 &&…), it doesn’t. But the version with bitwise operators, as I said, works fine. What’s the difference? Why it doesn’t work? What am I missing? Thanks for any advice.
you can use arr.every() function which returns true if a condition is true for all the elements of the array or you can use a flag variable, which should be initialized in outer loop and if the condition is true for all the elements in second loop then change the flag’s value, then below the second loop use if statement to test the flag value
So loop (i) will iterate until the condition for all arr indexes are met? So lets say (i) has to iterate 1000 times, will it return number 1000?
are you trying to solve smallest common multiple?
Yup. And as I said, it works with nitwise operators, but can’t automate it and it looks terrible. So I am trying to find a way. I’ll try your suggestions and will see if it works or not.
best of luck though you have to post your code so that people can assist you
Yeah, I know, but I don’t have access to PC right now, so I tried to describe it as goos as possible.
You must be using mobile phone ? you can do on mobile phone whatever you can do on a pc I suppose
Yup, just not enough time to write the code.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.