第一个函数找到一个数字的因子并且工作正常。
//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>
它不会返回任何东西,第二个功能是让我麻烦的那个。我一直在看它一个小时。开始自己学习编程。 谢谢您的帮助。
答案 0 :(得分:2)
问题是您正在访问这样的数组项:verticalViewPager.setPageTransformer(false, new ViewPager.PageTransformer() {
@Override
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position >= 0) {
view.setTranslationY(position < 0 ? 0f : -pageHeight * position);
}
}
});
。
你应该用方括号来做:
arr(i)
你根本不需要圆括号。另外,您最好use strict comparison(if ((arr1[i])==(arr2[j])){
common.push(arr1[i]);
//...
}
代替===
)。
所以它会是:
==
BTW考虑检查this question
答案 1 :(得分:2)
首先,if ((arr1[i])==(arr2[j]))
代替if ((arr1(i))==(arr2(j)))
其次,删除var arr1=[]; arr2 =[];
作为接收参数,然后离开var common=[]
我也会在循环中使用m
,因为我猜这是你的意图
答案 2 :(得分:1)
正如我已经评论过的那样,您正在重置参数,因此无法正常工作。请检查以下代码。
//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)));
&#13;
<!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>
&#13;