I got the code below working, but when I try to add a Browser.msgBox() once there is a duplicate in the comparison, the code keeps running until it exceeds its time limit.
The idea is to notify the user that the item he/she is trying to add is duplicated and have the script stop running.
var duplicate = false;
for(var x = 0; x < data.length; x++) {
for(var j = 0; j < dataArquivoItens.length; j++){
if(data[x].join() == dataArquivoItens[j].join()){
duplicate = true;
break;
}
}
}
Thanks a lot!
You are only breaking out from the if statement, this is why your code keeps iterating
If you want to break from all nested loops/ statements - give them a name
Sample:
var duplicate = false;
loop1:
for(var x = 0; x < data.length; x++) {
loop2:
for(var j = 0; j < dataArquivoItens.length; j++){
if(data[x].join() == dataArquivoItens[j].join()){
duplicate = true;
Browser.msgBox("That's a duplicate");
break loop1;
}
}
}
Related
This is from Codecademy's Javascript lesson "Search Text For Your Name". The following works:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < i + myName.length; j++) {
hits.push(text[j])
}
}
}
However, when I replace i + myName.length with j + myName.length, it's crashing. In full:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for ( i=0; i < text.length; i++ ){
if (text[i] === myName[0]) {
for (var j = i; j < j + myName.length; j++) {
hits.push(text[j])
}
}
}
I'm not getting any errors when I run this, which led me to believe that it's just stuck in an infinite loop, except that when I place a console.log marker within the For loop in question, it doesn't print anything.
What's the reason for it crashing?
j < j + myName.length; j++
j never reaches the end. You're incrementing it, but you compare it against number that is always larger than itself (assuming myName.length is > 0). The conditions for the loop is always satisfied, causing it to run forever.
It crashes because it's an infinite loop.
Here is your second example, with the static variables converted to their values:
var text = "My name is Zorak. Everyone calls me Zorak.";
var myName = "Zorak";
var hits = [];
for (i = 0; i < 42; i++) {
if (text[i] === 'Z') {
for (var j = i; j < j + 5; j++) {
hits.push(text[j]);
}
}
}
Specifically, your inner for condition is causing the infinite loop:
for (var j = i; j < j + myName.length; j++) {
or with myName.length replaced by its value, 5:
for (var j = i; j < j + 5; j++) {
j will always be less than j + 5 so the loop continues without end, consuming memory until crash.
This is suppose to store the chosen answer for multiple questions. When I use this code, it only checks the first question and disregards the other questions.
for(i = 0; i < questions.length-1; i++){
radios = document.getElementsByName(questions[i]);
for (var t = 0; length < radios.length; t++) {
if (radios[t].checked) {
var qResults = JSON.parse(localStorage["qResults"]);
num = radios[t].value;
checked = num.toString();
var temp = (id[0] + ";" + questions[i] + ";" + checked);
alert(temp);
qResults.push(temp);
localStorage["qResults"] = JSON.stringify(qResults);
}
}
alert("question finished");
}
Your inner loop is wrong. Change this:
for (var t = 0; length < radios.length; t++) {
to:
for (var t = 0; t < radios.length; t++) {
Side note: I would suggest that you read the local storage before the loops, and write it back after the loops, instead of doing it for every question.
In addition to Guffa's fix, I think it makes more sense if you can move var qResults and localStorage["qResults"] outside of the second for loop:
var qResults = JSON.parse(localStorage["qResults"]);
for loop I {
for loopII {}
}
localStorage["qResults"] = JSON.stringify(qResults);
I am writting a simple metaballs implementation in JS. I have an array of the metaballs and i iterate through all of them every frame, and for each one I check the distance to every other metaball and if they are close enough I need to merge them.
This is how I guess it could look, but I don't know how to properly remove the element from array and not break the loops.
for(var i = 0; i < points.length; i++) {
for(var j = 0; j < points.length ; j++) {
if(i != j) {
if(distance < 10) {
//remove one of the points using splice
}
}
}
}
Thanks for help.
First, start your inner loop at i + 1. You've already compared elements up to i, so there's no need to repeat, right? That let's you get rid of your if statement as well.
Then, when you splice, decrement j so as to not skip the next element.
for(var i = 0; i < points.length; i++) {
for(var j = i + 1; j < points.length ; j++) {
if (distance(i, j) < 10) {
points.splice(j--, 1);
}
}
}
var text = "Hue, bla, hue, rhr, aek kmggg mlsd k Bjarni sdkrals fn lol Bjarni\
lelelele Bjarni";
var myName = "Bjarni";
var hits = [];
for(var i = 0; i < text.length; i++); {
if (text[i] === "B"); {
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
}
}
console.log(hits);
This is supposed to return my name, but it just returns an array containing a few undefined values.
The problem is here:
if (text[i] === "B"); {
Get rid of the semicolon. The semicolon in that point of the code makes it like:
if (text[i] === "B")
; // DO NOTHING AT ALL
{ // START A BLOCK
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
}
edit — oh and there's another one (with similar effects) after the for loop header. It should be:
for(var i = 0; i < text.length; i++) {
You've just got a few syntactical errors and a missing break statement to fix. This should work:
var text = "Hue, bla, hue, rhr, aek kmggg mlsd k Bjarni sdkrals fn lol Bjarni\ lelelele Bjarni";
var myName = "Bjarni";
var hits = [];
for(var i = 0; i < text.length; i++) {
if (text[i] === "B") {
for(var j = i; j < (myName.length+i); j++) {
hits.push(text[j]);
}
break;
}
}
console.log(hits);
To clarify; you had a semicolon after the line if( text[i] === "B");, another semicolon after your first for loop and you were missing a break statement, so it would've looped around throughout the entire "text" string, which may or may not have been unintentional behaviour.
i have function:
function getFieldNames(arrayOfRecords) {
var theStuff;
for (var i = 0; i = arrayOfRecords.length - 1; i++){
theStuff = arrayOfRecords[i];
theList = theStuff.split('" ');
for (var j = 0; j = theList.length - 1; j++) {
var v = theList[j].split('="');
fName1[i][j] = v[0];
}
}
return fName1;
}
the argument arrayOfRecords is an array, and i dont know how to setup to the 'theStuff' variable an array element? When I do like it is above, i get something stupid.
can anyone help me? :)
There may be other problems but the one that leaps out at me is your for loop header:
for (var i = 0; i = arrayOfRecords.length - 1; i++)
The second part should be a condition, which when evaluated to false will stop the loop from running. What you probably wanted was:
for (var i = 0; i < arrayOfRecords.length; i++)
So when i is not less than arrayOfRecords.length, the loop will stop. Alternatively (to keep the - 1, but I tend to use the above version):
for (var i = 0; i <= arrayOfRecords.length - 1; i++)
The same goes for the nested loop.