Skip Iteration Google Apps Script - javascript

I have a very simple For Loop in Google Apps Script to check some conditions in a Google Sheet. What I want is to add another condition, if it is met, then I want to skip current iteration and move on to next. This is pretty easy in VBA, but I am not sure how to do it on JavaScript.
Current code:
for (var i=1 ; i<=LR ; i++)
{
if (Val4 == "Yes")
{
// Skip current iteration... <-- This is the bit I am not sure how to do
}
elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
{
// Do something..
}
else
{
// Do something else...
}
}

continue statement can be used to continue to next itteration :
for (var i=1 ; i<=LR ; i++)
{
if (Val4 == "Yes")
{
continue; // Skip current iteration...
}
// Do something else...
}
In your sample case, leaving the if block empty will achieve the same result:
for (var i=1; i <= LR; i++)
{
if (Val4 == "Yes")
{
}
elseif (Val1 == "Accepted" && !(Val2 == "") && !(Val3 == ""))
{
// Do something..
}
else
{
// Do something else...
}
}

Related

How to make multiple conditions inside single filter

I am trying to make a filter based on checkboxes.
The thing is js ignoring other conditions inside filter when one is active
filterData() {
return this.airlines.filter(x => {
if (this.filters.options.length != 0 || this.filters.airlines.length != 0) {
for (let i = 0; this.filters.options.length > i; i++) {
if (this.filters.options[i] == 0) {
return x.itineraries[0][0].stops == 0;
}
if (this.filters.options[i] == 1) {
return x.itineraries[0][0].segments[0].baggage_options[0].value > 0;
}
}
} else {
return x;
}
})
}
I know that return will stop the current loop, but is there any way to do it correctly?
Update-1: (When to filter record for every case checked OR case)
Replace for loop and all conditions in a single return by && for if and || condition for data:
var chbox = this.filters.options;
return $.inArray(0, chbox) != -1 && x.itineraries[0][0].stops == 0
|| $.inArray(1, chbox) != -1 && x.itineraries[0][0].segments[0].baggage_options[0].value > 0;
Hope this helps !!
$.inArray(value, arr) method will check for each checkboxes and will work for every checked ones .
Update-2 (When to filter record for every case checked AND case)
As per comment below, you are trying to use checkbox on demand so use below code:
var chbox = this.filters.options;
boolean condition = true;
if ($.inArray(0, chbox) != -1) {
conditon = conditon && x.itineraries[0][0].stops == 0;
}
if ($.inArray(1, chbox) != -1) {
conditon = conditon && x.itineraries[0][0].segments[0].baggage_options[0].value > 0;
}
return condition;
Your filter function is returning an object, which ideally should be a boolean value. Please refactor the code as below.
filterData() {
return this.airlines.filter(x => {
let result = false;
if (this.filters.options.length != 0 || this.filters.airlines.length != 0) {
for (let i = 0; this.filters.options.length > i; i++) {
if (this.filters.options[i] == 0) {
result = x.itineraries[0][0].stops == 0;
break;
} else if (this.filters.options[i] == 1) {
result = x.itineraries[0][0].segments[0].baggage_options[0].value > 0;
break;
}
}
}
return result;
})
}

logic in if/else method in javascript

i just want to implement if else method in my apps
the problem is when in choose the first if it working, but if i choose second if its not working, this is my code
//filter in session
filterMarkersx = function (session) {
var table = $('#edcTable').DataTable().column(1).column(2);
var str;
$("select#type option:selected" ).each(function() {
str = $(this).val();
});
for (i = 0; i < gmarkers1.length; i++) {
marker = gmarkers1[i];
if(marker.session == session || session.length === 0) {
marker.setVisible(true);
} else if(marker.session == session || session.length === 0) {
if (marker.category == str || $("#type").length === 0){
marker.setVisible(true);
}
}else{
marker.setVisible(false);
infowindow.close(map, marker1);
}
}
table.search(str).draw();
}
anyone can help me what must i do, thanks in advance.
-kraken.
Your if and else if conditionals are exactly the same. The else if block can never execute, since if its conditional is true, then the first if is true, so its else isn't even looked at.

Multiple if statements are executing in succesion?

I have many if statements the are supposed to trigger on a left or right key press. But when I hit left, it just executes the left key press on all the if statements, even though there are conditons for each statement.
var currentBranch = 1;
if ((currentBranch == 1) && (keyPressed[key.left] == true)){
background.image.src = treeStructure[0][0];
currentBranch = 3;
console.log(currentBranch);
} else if ((currentBranch == 1) && (keyPressed[key.right] == true)) {
background.image.src = treeStructure[0][1];
currentBranch = 2;
console.log(currentBranch);
}
if ((currentBranch == 3) && (keyPressed[key.left] == true)){
background.image.src = treeStructure[1][0];
currentBranch = 4;
console.log(currentBranch);
} else if ((currentBranch == 3) && (keyPressed[key.right] == true)) {
background.image.src = treeStructure[1][1];
currentBranch = 9;
console.log("hello");
console.log(currentBranch);
}
if ((currentBranch == 4) && (keyPressed[key.left] == true)){
background.image.src = treeStructure[2][0];
currentBranch = 6;
console.log(currentBranch);
} else if ((currentBranch == 4) && (keyPressed[key.right] == true)) {
background.image.src = treeStructure[2][1];
currentBranch = 5;
Shouldn't the currentBranch variable stop it after each if statement, for a new key press?
As written, your code is actually 3 separate if... else if... blocks, not one set of chained if blocks. This means that their "truthiness" will be evaluated individually, rather than breaking out after one has evaluated to true.
The first "if..." block evaluates to true, and then inside of that block, you set the variable "currentBranch" to 3, which causes the next if block to evaluate to true, and so on down the line.
You need to change the structure of your code to this:
if (statement) {
//code
}
else if (statement) {
//code
}
else if (statement) {
//code
}
else if (statement) {
//code
}
else if (statement) {
//code
}
else if (statement) {
//code
}
This way, the lower blocks won't be evaluated once a block has evaluated to true.

Cookie seems not to work

Since i needed to have some variables of my programs available in all the pages, i decided to use cookies (js-cookie).
if ((Cookies.get('j') == null) || (Cookies.get('j') == 0)) {
Cookies.set('j', 0);
var j = parseInt(Cookies.get('j'));
}
Cookies.set('imuno');
var imuno = Cookies.get('imuno');
Cookies.set('imdue');
var imdue = Cookies.get('imdue');
Then i wrote two functions:
function check1(){
if ((src1 == "") || (src1 == "undefined")) {
alert("Selezionare un'immagine.");
}
else {
controllo();
}
}
and
function check2(){
if ((src2 == "") || (src2 == "undefined")) {
alert("Selezionare un'immagine.");
}
else {
controllo();
}
}
As you can see, they're two check functions that retrieve another function (controllo()) which works like that:
function controllo() {
if (j == 0) {
alert(j);
imuno = src1;
Cookies.set('imuno', src1);
alert(imuno);
location.href = "schienale.html";
j++;
Cookies.set('j', 1);
}
else if (j == 1){
alert(j);
imdue = src2;
Cookies.set('imdue', src2);
alert(imuno,imdue);
location.href = "riep.html";
j++;
Cookies.set('j', 2);
}}
All this code is written in my external .JS file. Now the funcion check1() works perfectly, but when the check2() start running the program doesn't work anymore, as if the cookie j doesn't mantain its value. How can i solve? Thanks all!

IF- ELSE condition - run the code in the ELSE section

I have got the following IF condition code:
if ((depth <= min_depth ) && (leaf_colour == "red")){
for (i = 0; i < array_2D.length; i++) {
var leaf_size = array_2D[i][1];
if (leaf_size == 10 || leaf_size == 11){
alert("Error message.");
break; // we found an error, displayed error message and now leave the loop
}
else{ go to the next else section }
}
}//end of if condition
else{
...
...
...
...
...
}
Inside the 'FOR' loop, if (leaf_size == 10 || leaf_size == 11), we break the loop and do nothing but if this is not the case, i would like to run the code in the next ELSE section.
I do not want to copy the whole block of code and paste it inside the 'else' section of the for loop as it's quite long.
Is there a way of running the code in the second else section?
You will need to move the code from your second else block into a separate function. You can then call that function wherever you need to run that code:
function newFunction() {
//Shared code. This is executed whenever newFunction is called
}
if(someCondition) {
if(someOtherCondition) {
//Do stuff
}
else {
newFunction();
}
}
else {
newFunction();
}
var ok = (depth <= min_depth ) && (leaf_colour == "red");
if (ok){
for (i = 0; i < array_2D.length; i++) {
var leaf_size = array_2D[i][1];
if (leaf_size == 10 || leaf_size == 11){
alert("Error message.");
ok = false;
break;
}
else{
ok = true;
break;
}
}
}//end of if condition
if(!ok) {
...
...
...
...
...
}

Categories

Resources