admin管理员组文章数量:1323342
var array = [3, 4, 6, 5, 9, 10, 21];
var divThree = [];
var loveTheThrees = function (array) {
for (i = 0; i = array.length; i++) {
if (array[i] % 3 === 0) {
amount.push(divThree[i]);
}
}
}
I am just learning Javascript and having a bit of trouble with this function. I can feel im close but cant figure out what im doing wrong.
var array = [3, 4, 6, 5, 9, 10, 21];
var divThree = [];
var loveTheThrees = function (array) {
for (i = 0; i = array.length; i++) {
if (array[i] % 3 === 0) {
amount.push(divThree[i]);
}
}
}
I am just learning Javascript and having a bit of trouble with this function. I can feel im close but cant figure out what im doing wrong.
Share Improve this question asked Aug 1, 2016 at 18:53 Josh-HarrisJosh-Harris 313 bronze badges 2-
Note: you should write
for (var i = 0
instead offor (i = 0
– gcampbell Commented Aug 1, 2016 at 18:55 - what is your error? What state are you trying to hit now? – joncodo Commented Aug 1, 2016 at 19:00
6 Answers
Reset to default 8Just use filter()
divThree = array.filter(x => x%3 === 0);
There are different ways of going about it (as evidenced by the other answers here), but the bug in the code you posted is that the i = array.length
is resetting the value of i
. You'll want to use a <
instead. You also need to push into the proper array.
var array = [3, 4, 6, 5, 9, 10, 21];
var divThree = [];
var loveTheThrees = function (array) {
for (i = 0; i < array.length; i++) {
if (array[i] % 3 === 0) {
divThree.push(array[i]);
}
}
}
You can use .filter
of array
var array = [3, 4, 6, 5, 9, 10, 21];
var output = array.filter(function(num){
return num%3 ==0
});
console.log(output);
In your code you are assign i = array.length
instead of a condition, so your loop is infinite. You need to change =
into the <
. And also push into the dimThree
not to amount
.
var array = [3, 4, 6, 5, 9, 10, 21];
var divThree = [];
var loveTheThrees = function (array) {
for (i = 0; i < array.length; i++) {
if (array[i] % 3 === 0) {
divThree.push(array[i]);
}
}
}
loveTheThrees(array);
divThree.forEach(x => console.log(x));
Or use filter()
function
var array = [3, 4, 6, 5, 9, 10, 21];
var divThree = [];
divThree = array.filter(x => x % 3 === 0);
divThree.forEach(x => console.log(x));
you will have to push the right value:
divThree.push(array[i]);
and: you can implement a for loop like:
for (var i = 0; i < array.length; i++) { .. }
or
for (var i = 0, l = array.length; i<l; i++) { .. }
You first need to correct the for-loop condition from i=array.length
to i < array.length
. But even more important in this case - you need to consider the following:
You want to add/push the values that are divisible by 3 into an array called divThree
- so the correct syntax is <destination-array>.push(<value-to-push>)
. So in this case, if the current value array[i]
is divisible by three you want to push it into the array called divThree
so you do something like this:
for (i = 0; i < array.length; i++) {
if (array[i] % 3 === 0) {
//here we push array[i] into the array because it is divisible my 3
divThree.push(array[i]);
}
}
As you progress in your Javascript, you will find that in this case, it is easier to use lodash (for NodeJS apps) as it provides very nice convenience functions of these kinds of things.
I hope this helps.
本文标签: javascriptmaking a function that pulls all number from an array divisible by 3Stack Overflow
版权声明:本文标题:javascript - making a function that pulls all number from an array divisible by 3 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135008a2422330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论