i want to know what happens when we don't use it
eg
for(;i<=10;i++)
{
}
I searched some sites but i am very confused.
Thanks.
Well first off, it will give an error saying that i is not defined. But if you declare it ahead of the loop, it works fine:
var i = 0;
for(;i<=10;i++){
console.log(i);
}
You can also remove the termination statement as well, and keep it in the loop like this :
var i = 0;
for(;i<=10;){
console.log(i);
i++;
}
It'll work, I hope that answers your question...
Its similar to a while loop where you initialize a variable first and then keep the loop with the comparison and the terminate it with a termination statement. So according to the code in your question, what you are basically do is something like this:
while(i <= 10){
console.log(i);
i++;
}
Which will give an error.
Related
I have a for loop which will iterate through all choices and and set a value for them.
For some reason, when I run the code it does the first iteration, then skips to the last and does that one 3 times, or so according to the console.
code:
for (i = 0; i < 4; i += 1) {
console.log(i)
var generated = word
while (generated == word) {
generated = wordsJson.characters[Math.floor(Math.random() * wordsJson.characters.length)]
}
choices[i].innerHTML = translate(generated)
}
What I get in console:
0
(3) 3
This is my first time asking something on stackoverflow. If you need more information, please ask.
It appears that the variable i is getting modified outside of the for loop.
Typically you would want to declare your iterator variable (in this case i) so that it's scoped to the loop, which would look like:
for (let i = 0; i < 4; i += 1) { ... }
Note, specifically, the addition of let. Since you haven't done that, it means that either i is already explicitly declared somewhere or, if not, that you've created a new global variable i.
Since you've got code you haven't shown that also seems to relate to i (choices[i]) and methods that we don't know the exact function of (translate()) it's hard to say for certain, but that would be the first place to look.
If not, posting some additional code so we can see the other functionality would be helpful.
I had a problem which was that I was using a loop e.g
for(let i=0; i<10; i++){
if(i === 3){
// go to the next iteration of the loop
}
console.log(i)
}
and I was struggling to see how to get to the next iteration. I tried a "return" statement but this came up with the error "illegal return statement" and having done a quick search on the forums the answer was not obvious, so I thought I'd log it here so that next time I can find it easier.
Having looked through the MDN docs, what IU actually wanted was a "continue" statement which short-circuits the rest of the code in the block and goes straight to the next iteration.
You just have to write a continue keyword in for loop to goto next loop,
for(let i=0; i<10; i++){
if(i === 3){
continue;
}
console.log(i)
}
Note: choose let judiciously, it can also be used with var as it is having a function scope.
If you want to skip the rest of code in current iteration and get to next one, continue;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
If you want to exit loop completely, use break;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
During the course of designing a small API at work and trying to make my functions as flexible as possible I decided to start adding checks for whether arguments are passed, and then based on that, do different things. So when the function is called with a number, the function uses the number as an index in an array. If no number is passed, I wanted the function to call itself as many times as the length of the array. However I get the call stack error. I have boiled the problem down to the recursion aspect of the function, which I'm listing below. The thing that is strangest to me is this...
THIS CAUSES ERROR
function testing(a){
if(!a){
for(var i = 0; i < 3; i += 1){
testing(i);
}
}else{
alert(a);
}
}
testing();
THIS DOES NOT CAUSE ERROR
function testing(a){
if(!a){
for(var i = 0; i < 3; i += 1){
testing(5);//Just adding hard coded number instead
}
}else{
alert(a);
}
}
testing();
I'm trying to understand why passing the var in the call throws an error. It seems that if the js engine can hold the initial function call in memory in order to make the for loop work properly why couldn't it hold a reference to i while calling itself? I feel like I am missing something fundamental here. I've tried lots of rewrites involving things like:
testing(function(i){return i;}(i));
All to no avail. This is driving me crazy and I would like to understand what is going on here.
if a===0 it is false, that means you have an infinite loop
for more details, read this and this (thanks #yochannah )
This works just fine.
function testing(a){
if(!a || a == 0){
for(var i = 1; i <= 3; i++){
testing(i);
}
} else {
alert(a);
}
};
testing();
Obviously there's no need to both start the loop counter off of 1 AND checking whether a == 0, but both methods would work.
EDIT: Another check you can have is (a == undefined).
The thing is that a variable that returns 0 is false when you check it with !.
I'm currently learning javascript and I'm a little confused on why my do-while loop isn't working. Maybe I'm missing a core concept of how it's supposed to work, but I just don't seem to get why this isn't working. I could use a for loop or regular while loop to iterate through this list, and I understand those fine, but I'd like to figure it out using a do-while loop. Can anyone explain what is wrong?
i=0
var names = ['Bobby', 'Chris', 'John', 'Joe'];
do{
i+=1
}
while(i < 4){
console.log(names[i]);
}
The syntax should be with everything between the do and while braces:
do{
console.log(names[i]);
i+=1
} while(i < 4);
(Fiddle)
Your syntax is wrong, when you're using do{ }while() the while part doesn't take any braces, there's no need to.
Think of Do..while blocks as "Shoot first, ask later"
In other words, you execute a block of code first, and then you figure out if the condition is still true.
The reason you're getting an undefined output is because you're trying to access an undefined element.
JavaScript arrays are zero based, it means that if you have an array with 4 elements you're going to have indices going from 0 to 3
Because of the nature of do{ } while() you're incrementing your i BEFORE testing whether or not you've reached the end of the array, therefore when your loop ends, your i has the value of 4
and you're asking javascript for the value of names[4] which doesn't exist, and you end up with an undefined console output.
How to fix this?
It depends on what you're trying to achieve. Your code seems to me like you're trying to access the last element on your array, if that's what you're trying to do change your current condition form:
while(i < 4) to while(i < 3) and your code will work
Don't take my word for it, here's a fiddle for you:
http://jsfiddle.net/n8exp/
There is continue; to stop the loop and move to the next loop
There is break; to stop the loop and move to the end of the loop
Isn't there some kind of start; that stop the loop and move to the beginning of the loop?
I know it is easy to achieve all of these three actions by just modifying the value of i, but I always try to look for already built-it functions.
Resetting the value of your loop variable to the initial value then calling continue is as close as you'll get.
For example:
for(var i=0; i<20; i++) {
if(somecondition) {
i=-1; continue;
}
}
No - there is no keyword or other way to do it automatically.
As you already mentioned you can just modify the loop condition variable(s) within your loop. Easy if it's a simple i counter, but of course you may have more initialisation to do than just a simple counter.
Or you can do something like the following:
restartLoop:
while (true) {
for (var i=0, j=100000, x="test"; i < 1000; i++, j--, x+= ".") {
if (/*some condition, want to restart the loop*/)
continue restartLoop;
}
break;
}
The continue restartLoop will jump back out to continue with the next iteration of the while loop, which then immediately starts the for loop from the beginning including all of the initialisation code. If the for exits normally the break statement after it will break out of the containing while loop.
I don't really recommend doing this in a general sense, but if your loop initialisation process was really complicated it could be worth it because then you wouldn't need to repeat it all inside the loop. If you needed to do even more initialisation than fits nicely in the for statement's initialisation expression you can easily put it just before the for loop inside the while and it will all be re-run...
If you want to avoid jumps or the equivalent of goto statements that many of us have been trained to avoid, you could use a local function for the loop and a test on the return value to see if you should just call it again:
function doItAll() {
// put state variables other than the actual loop control here
function doTheLoop() {
for(var i=0; i<20; i++) {
if (somecondition) {
return(true); // run the loop again
}
}
return(false); // done running the loop
}
while (doTheLoop()) {}
// do some things after the loop
}
No. (Just to rule out a "I just haven't heard of it, either" - it isn't mentioned at https://developer.mozilla.org/en/JavaScript/Reference/Statements.)
continue works by simply skipping the rest of the loop body. break works by skipping the rest of the loop body and then ending the loop. A start function would have to somehow "rewind" the state of the program - but not all of the state of the program, since presumably you don't want to lose what you did, either - to where it was when the loop began, which is not something that any programming language that I have seen provides.
You could have the loop in a function that calls itself recursively:
function loopIt(numTimes) {
if (numTimes < 3) {
for (x = 0; x < 20; x++) {
if (x == 5) {
loopIt(numTimes+1);
break;
}
}
}
}
You can obviously change the conditions to fit you logic as the above is a simple example.