编程新手,无法弄清楚为什么这个因子计算器不起作用
New to programming, can't figure out why this factor calculator doesn't work
我正在尝试制作一个因子计算器。您输入一个数字,它会找出该数字的因数。如果将原始数字除以它的因子,你会得到零,我正在尝试在此处实现它,以便当它 returns 和 '0' 时它被推送到一个数组并打印该数组。
var number = prompt("Number?")
var array = []
function modulo(a, b)
{
return a % b;
}
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result = 0)
{
array.push(counter)
}
}
for (counter = 0; counter < array.length; counter++)
{
alert(array[counter])
}
提示出现了,我输入了一个数字,但没有任何反应。有人可以帮忙吗?
问题来了,你用了=
(赋值运算符)而不是==
比较运算符
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result == 0) // in your code this is result = 0
{
array.push(counter)
}
}
完整代码:
var number = prompt("Number?")
var array = []
function modulo(a, b)
{
return a % b
}
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result == 0)
{
array.push(counter)
}
}
for (counter = 0; counter < array.length; counter++)
{
alert(array[counter])
}
要检查值是否相等,请使用 ==
而不是 =
:
if (result == 0)
我正在尝试制作一个因子计算器。您输入一个数字,它会找出该数字的因数。如果将原始数字除以它的因子,你会得到零,我正在尝试在此处实现它,以便当它 returns 和 '0' 时它被推送到一个数组并打印该数组。
var number = prompt("Number?")
var array = []
function modulo(a, b)
{
return a % b;
}
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result = 0)
{
array.push(counter)
}
}
for (counter = 0; counter < array.length; counter++)
{
alert(array[counter])
}
提示出现了,我输入了一个数字,但没有任何反应。有人可以帮忙吗?
问题来了,你用了=
(赋值运算符)而不是==
比较运算符
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result == 0) // in your code this is result = 0
{
array.push(counter)
}
}
完整代码:
var number = prompt("Number?")
var array = []
function modulo(a, b)
{
return a % b
}
for (counter = 0; counter < number; counter++)
{
var result = modulo(number, counter)
if (result == 0)
{
array.push(counter)
}
}
for (counter = 0; counter < array.length; counter++)
{
alert(array[counter])
}
要检查值是否相等,请使用 ==
而不是 =
:
if (result == 0)