简体   繁体   English

查找数组的交集以查找hcf

[英]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) . 问题是您正在访问如下数组项: 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))) 首先,用if ((arr1[i])==(arr2[j]))代替if ((arr1(i))==(arr2(j)))

Second, remove var arr1=[]; arr2 =[]; 其次,删除var arr1=[]; arr2 =[]; var arr1=[]; arr2 =[]; as they are received parameters, and leave var common=[] 因为它们是接收到的参数,并保留var common=[]

I would also use m in the loop as I guess that was your intention 我也会在循环中使用m ,因为我猜这是您的意图

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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