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.
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?
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”.
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.