if i use return i; it only gives me the result of 1, but when i use console.log(i) it goes thru the whole list can would be helpful if someone please explain why it isn't working ?
Code:
var loop = function(){
for( var i = 1; i < 100; i++ ){
if(i % 5 === 0 || i % 7 === 0){
} else {
console.log(i) ;
}
}
};
loop();
"return" exits the function completely and doesn't continue executing the loop....console.log only appends your content to the console and doesn't exit the function at all...
@nap0leon: I know, i meant it in place of console.log, however that is not the idea, the idea is to print the whole number list and skip those divisable by 5 and 7, the console.log(i) did it correct but i was just trying to understand return and why it didn't work.
i'am testing it on codeacademy's console by the way.
thanks
@criterion9: Really ? i didn't find that info in any reference for return, anyway thanks for clearing THAT up, know where i can find more info about it ?
@nap0leon: I know, i meant it in place of console.log, however that is not the idea, the idea is to print the whole number list and skip those divisable by 5 and 7, the console.log(i) did it correct but i was just trying to understand return and why it didn't work.
i'am testing it on codeacademy's console by the way.
thanks
@criterion9: Really ? i didn't find that info in any reference for return, anyway thanks for clearing THAT up, know where i can find more info about it ?
If you want to return the entire list of numbers, you would have to pass back an array (or if you want to use a string, but that isn't as useful):
Code:
function loop()
{
var list = [], i;
for (i = 1; i < 100; ++i) {
if (i % 5 && i % 7) {
list.push(i);
console.log(i);
}
}
return list;
}
alert(
loop()
);
Bookmarks