1

The problem is best explained by the comments in my code.

// Find all commonNums divisible by arr && sequential that produce a whole number quotient.
// commonNums [ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75]
// arr [1,5]
// sequential [ 2, 3, 4 ]
for (var n = 0; n < commonNums.length; n++) {
  for (var o = 0; o < sequential.length; o++) {
    for (var p = 0; p < arr.length; p++) {
  if (commonNums[n] % arr[p] === 0 && commonNums[n] % sequential[o] === 0) {
  console.log(commonNums[n]);
}}} 
} 

Since the arrays are of different length, simply iterating through with one loop of length commonNums.length produced undefined values. My solution was to use 3 loops, one for each array.

If common nums divided by arr has no remainder, and if common nums divided by sequential has no remainder, then return that number. For arr [1,5] the first number returned should be 60.

Why does this solution fail?

5
  • What errors are you seeing? Commented Mar 29, 2016 at 20:50
  • Shouldn't the first number returned be 10? After all 10 % 1 == 0 and 10 % 2 == 0 Commented Mar 29, 2016 at 20:50
  • I don't get an error, but I do get 10 as the first number as suggested by entropic. 10, 10, 15, 15, 20, 20, 20, 20, 30, 30, 30, 30, 40, 40, 40, 40, 45, 45, 50, 50, 60,60,60,60,60,60, 70, 70, 75, 75. Commented Mar 29, 2016 at 20:54
  • According to your first comment you should find all numbers in commonNums which are divisible by arr && sequential. Doesn't it mean that you should print number only if it divisible by all number in arr and sequential? Commented Mar 29, 2016 at 20:55
  • The code does exactly as it is written and runs without issue. If your output isn't right then you haven't defined your problem correctly. Commented Mar 29, 2016 at 20:58

3 Answers 3

2

If I understand your problem correctly then this should work. It returns 60 as that is the only number that is divisible by every number in the other two arrays.

outerLoop:
for (var n = 0; n < commonNums.length; n++) {
    for (var o = 0; o < sequential.length; o++) {
        for (var p = 0; p < arr.length; p++) {
            if (commonNums[n] % arr[p] != 0 || commonNums[n] % sequential[o] != 0) {
                continue outerLoop;
            }
        }
    }
    console.log(commonNums[n]);
}

Updated to use a lablel/continue to speed up execution.

Sign up to request clarification or add additional context in comments.

3 Comments

Kudos for figuring out what he wanted - but you could really speed up execution by adding break statements once match == false
Can you please explain entropic?
What he was saying was that after the code figured out that one of the numbers in either arr or sequential didn't work my old code would continue to check all the remaining numbers even though it was unnecessary. The new code simply jumps to the next number in commonNums.
0

Using 3 for loops is the (only) way to go if you need to generate all possible 3-tuples of 3 different arrays. (But keep in mind that this won't be efficient if you are dealing with large amounts of data)

The problem I see with your solution is that some numbers are displayed twice. If you only want to put them in an array (for instance to return them or print them later), just check it is not already in the array using Array.indexOf(), and if not add it.

2 Comments

Since Array.indexOf() has a complexity of O(n) in the worst case it would be O(m * n * p) time and space so it could be very expensive with large data sets, I'd suggest writing in a hash so that it becomes O(1) (but would still be the same complexity in space).
Yes, that's indeed true. Using an object would be a better option, for instance: pastebin.com/3bpzQm5t
0

This does the trick (returns 60 as the first element in the filtered array) using a more functional approach.

function check(common, seq) {
  return common.map(function (el) {
    return seq.every(function (e) {
      return el % e === 0;
    });
  })
}

Get the result of comparing each set of arrays against commonNums...

var out = check(commonNums, arr);
var out2 = check(commonNums, sequential);

...and then filter out the numbers when the true values correlate in each index.

var out3 = commonNums.filter(function (el, i) {
  return out[i] && out2[i];
});

DEMO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.