# For loop undefined in js

``````function modulus(num){
var stored = [];
for (var i = 0; i < num; i++){
if ( num[i]%4 == 3){
stored.push(num[i]);
return stored;
}
}
}
console.log(modulus(100));
``````

I have no idea why it’s still show undefined when I create a function.

Hey @beerbluedevils!

The issue is here.

``````num [i]

//translation
100
``````

So the computer is confused about what to do with that information

2 Likes

How to write function in the right way?
When I write pure for loop it works well.
I try to fix the function many times but it doesn’t work.

What is the problem that you are trying to solve? Is this a homework assignment? A coding challenge?

Are you trying to find all of the numbers from 0-99 that when divided by 4 give you a remainder of 3?

If you could provide more details on what you want the function to accomplish that would help.

Thanks!

Hi @beerbluedevils.
Do you know what this code does?

``````num[i]%4 == 3
``````

You are passing 100 to `modulus` and then doing `100[i]` in the `if` condition. Don’t you see a problem there? Even if what you did was right, you are returning from an `if` condition. If the condition doesn’t evaluate to `true`, what will the function return?

1 Like

I try to write the function to find the number from 0-100 that modulus 4 and remain 3.

Change the above to `i % 4 == 3` and return `stored` after exiting the loop and make sure to populate `store` with the right values not `undefined`. Assuming by " modulus 4 and remain 3" you mean “gives remainder 3 when divided by 4”.

1 Like

Also if you want to include the number 100 then you will have to use the `<=` here

1 Like
``````var stored = [];
function mod(num){
for ( var i = 1; i <= num; i++){
if ( i%4 == 3){
stored.push(i);
}
}
return stored;
}
console.log(mod(100));
``````

I can solved it now. The problem is I put the wrong condition in the first time and I put return statement before exiting loop. So I fixed that condition and put the return statement when exiting loop and it works. Thank you guys very much for help me solve this.

2 Likes