Javascript Break Statement Goodness [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to break from nested loops in Javascript?
Is using labels to break loops, a good practice in javascript? Mention if it has any pros and cons
Ex:
var i, j;
outer:
for(i in [0,1,2,3]) {
inner:
for(j in [0,1,2,3]) {
if(j == 1) {
break outer;
}
}
console.log("inner")
}
console.log("outer");

Yes, it is the best way to break out of multiple loops.

Related

Why this Insertion Sort method won't work if I change one VAR for LET in nested loops? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 4 months ago.
I'm currently doing a Javascript bootcamp. Today we were studying Sorting methods. The teacher provided us with "template" not-built-in functions. Another teacher also said that nowadays there's no reason to use VAR anymore to define a variable, that we should always use LET (and const, etc.).
So I copypasted his code in VSCode tu run it and study it. Then I decided to change all the VARs for LETs, and the code stopped working. I tried one by one and discovered it is one in particular (inside a loop that is nested in another loop) that if changed, then the code throws an error. So i would like to understand why is this happening? We've used lots of nested loops and this is the first time it happens.
Here is the code:
// INSERTION SORT
function insertionSort(arr){
let currentVal;
for(let i = 1; i < arr.length; i++){
currentVal = arr[i];
for(**var** j = i - 1; j >= 0 && arr[j] > currentVal; j--) {
arr[j+1] = arr[j]
}
arr[j+1] = currentVal;
}
return arr;
}
let hola = insertionSort([26,54,2,1,9,20,99,76,4]);
console.log(hola);
I wonder if this is related to the scope... If I delete this VAR, the code runs with no issue.... what's going on

best way to use "for in" [duplicate]

This question already has answers here:
Why is using "for...in" for array iteration a bad idea?
(28 answers)
JavaScript: Why does the "for in" loop pick variables from __proto__?
(4 answers)
Loop through an array in JavaScript
(46 answers)
Loop (for each) over an array in JavaScript
(40 answers)
Closed 8 months ago.
The code which worked before was failing now in for loop after I added enyo.js. I suspect that when I use for(var ls in list) it loops through even when length is 0. When I put the debugged I found out that it is considering "findIndex" as one of value in list and goes into the loop. I have several places using for with in, I want to find out a best way to filter out "findIndex" or any invalid indexes so that only valid elements go into the loop
for(var ls in list)
{
var lin = list[ls].rb ;
}
If you list is an array, just use a regular for loop. It's generally not a great idea to use for...in with an array for exactly this reason and also because the order isn't guaranteed.
If you must use for...in use a hasOwnProperty check:
for (var ls in list)
{
if (list.hasOwnProperty(ls)) {
var lin = list[ls].rb;
// ...
}
}
Of course, if you only concern is whether you have an rb property, you could just test for that:
if (list[ls].rb) {
var lin = list[ls].rb;
}
Or even:
var lin = list[ls].rb;
if (lin) {
// do whatever you needed to do with lin
}

Understanding asynchronous callbacks [duplicate]

This question already has answers here:
setTimeout in for-loop does not print consecutive values [duplicate]
(10 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 8 years ago.
I'm new to asynchronous programming, and I'm having a hard time getting the concept.
Please help!!!
Ive come up with a simple example:
for (var i = 1; i <= 10; i++) {
setTimeout(function () {
console.log(i);
}, 1000);
}
All I want is to print the indexes in ascending order but due to asynchronous action forced by the setTimeout i'm getting the last index printed out 10 times.
I understand why this happens...
No matter what I've tried (I don't think my misunderstanding needs elaboration) I failed solving this stupid riddle...
Im obviously missing something basic.
Please help me figure it out.
It's because all those functions use the same variable i, which is equal to 10 during invocation of them. Try something like this:
for (var i = 1; i <= 10; i++) {
setTimeout((function (k) {
return function(){
console.log(k);
}
}(i)), 1000);
}
It's because JavaScript has closures. You can read about them here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures

how do I create an infinite loop in JavaScript [duplicate]

This question already has answers here:
JavaScript Infinitely Looping slideshow with delays?
(11 answers)
Closed 8 years ago.
I want to create an infinite loop in JavaScript.
What are some ways to achieve this:
eg
for (var i=0; i<Infinity; i++) {}
You can also use a while loop:
while (true) {
//your code
}
By omitting all parts of the head, the loop can also become infinite:
for (;;) {}

Can we combine regular for loops with for in [duplicate]

This question already has answers here:
How to iterate over a JavaScript object?
(19 answers)
Closed 8 years ago.
I am curious but Google is not helping on this one...
We are looking for a tidy way of using an object key (it is an object containing functions) while also having clean access to the key names.
var obj={'key1':'val1','key2':'val2','key3':'val3'};
To get the desired key names in a loop we do: EDIT: this is wrong!
for(var i=0;i<obj.length;i++){
console.log(Object.keys(obj)[i]);
}
but would it be possible in this kind of loop?
for(var k in obj){
//?
}
I have seen combination loops before using &&. Is JavaScript able to do an elegant combination of both?
The closest I have got without disrupting the standard loop is:
var i=0;
for(var k in obj){
console.log(Object.keys(obj)[i]);
i++;
}
but it is hardly elegant, not very innovative, it is more of a work around because 'i' is declared outside of the loop. Everything else we have tried errors.
If I understand your question, it's the simplest ever thing.
for(var k in obj){
console.log(k);
}
Alternatively:
Object.keys(obj).forEach(function(key) { console.log(key); });
If you need the keys:
for(var k in obj) {
console.log(k)
}
If you need the values:
for(var k in obj) {
console.log(obj[k])
}
Are you trying to print out the keys and values?
for(var k in obj){
console.log(k,obj[k]);
}

Categories