简体   繁体   中英

finding intersection of arrays to find hcf

The first function find factors of a number and works fine.

 //first find divisors of a number function divisors(n) { var result = []; for (var i = 1; i <= n; i++) { if ((n % i) == 0) { result.push(i); } } return result; } //the following gives problems function commonTerms(arr1, arr2) { var arr1 = []; arr2 = []; common = []; var m = Math.min(arr1.length, arr2.length); for (var i = 0; i < arr1.length; i++) { for (var j = 0; j < arr2.length; j++) { if ((arr1(i)) == (arr2(j))) { common.push(arr1(i)); } else { continue; } } } return common; } var x = parseInt(prompt("number to find divisors of?")); document.write(divisors(x)); var y = parseInt(prompt("number to find divisors of?")); document.write("<br>" + divisors(y)); alert(commonTerms(divisors(x), divisors(y))); 
 <!DOCTYPE html> <html> <head> <link href="css/styles.css" rel="stylesheet"> </head> <body> <h1>GCD</h1> <p>This is my first website <br>finding div</p> </body> </html> 

It won't return anything, the second function is the one giving me trouble. I have been looking at it for an hour. Starting to learn programming on my own. Thank you for your help.

The problem is that you're accessing array items like this: arr(i) . You should do it with square brackets:

if ((arr1[i])==(arr2[j])){
      common.push(arr1[i]);
      //...
}

And you don't need the round braces at all. Plus you'd better use strict comparison ( === instead of == ).

So it would be:

if (arr1[i] === arr2[j]){
      common.push(arr1[i]);
      //...
}

BTW Consider checking this question

First, if ((arr1[i])==(arr2[j])) instead of if ((arr1(i))==(arr2(j)))

Second, remove var arr1=[]; arr2 =[]; var arr1=[]; arr2 =[]; as they are received parameters, and leave var common=[]

I would also use m in the loop as I guess that was your intention

As I have already commented, you are resetting parameters and hence its not working. Check following code.

 //first find divisors of a number function divisors(n) { var result = []; for (var i = 1; i <= n; i++) { if ((n % i) == 0) { result.push(i); } } return result; } //the following gives problems function commonTerms(arr1, arr2) { common = []; var m = Math.min(arr1.length, arr2.length); for (var i = 0; i < arr1.length; i++) { for (var j = 0; j < arr2.length; j++) { if ((arr1(i)) == (arr2(j))) { common.push(arr1(i)); } else { continue; } } } return common; } var x = parseInt(prompt("number to find divisors of?")); document.write(divisors(x)); var y = parseInt(prompt("number to find divisors of?")); document.write("<br>" + divisors(y)); alert(commonTerms(divisors(x), divisors(y))); 
 <!DOCTYPE html> <html> <head> <link href="css/styles.css" rel="stylesheet"> </head> <body> <h1>GCD</h1> <p>This is my first website <br>finding div</p> </body> </html> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM