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

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) {
...
...
...
...
...
}

Related

One if statement doesn't work and the rest do (Javascript)

So, I'm working on a game and the way to level up is through the money variable. I decided to use an interval and run the function upc every 0.099 seconds and when I run the code, you can get up to about at the point where it says if(money > 1000005 && Cookies.get*("modal2alrshown"). The Cookies.get is just a JS library.
function upc(){
if(money > 1000){
irp.style.display = "inline";
irptxt.style.display = "inline";
irpbtn.style.display="inline";
} else {
return false
}
if(money > 1005 && Cookies.get("modal1alrshown") != "yep"){
npamodal("./assets/iron_pickaxe.png","Iron Pickaxe")
Cookies.set("modal1alrshown","yep");
}
if(money > 1000000){
gp.style.display = "inline";
gptxt.style.display = "inline";
gpbtn.style.display="inline"
} else {
return false;
}
if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
npamodal("./assets/gold_pickaxe.png","Gold Pickaxe")
Cookies.set("modal2alrshown","yep");
} else {
return false;
}
if(money > 5000000){
bgep.style.display = "inline";
bgptxt.style.display = "inline";
bgpbtn.style.display="inline";
} else {
return false;
}
if(money > 5000005 ){
upcontent.innerHTML = "<div id='up1-close'>To exit, click on sky</div><h1 style='text-align:center;'>New Pickaxe Availible!</h1><img src='./assets/baguettepick.png' height='120px'>Baguette Pickaxe<br>And you got a <br><h3>Travel Ticket<img src='./assets/travelticket.png' height='20px'></h3>"
$("#up1-content, #up1-background").toggleClass("active");
Cookies.set("modal3alrshown","yep");
Cookies.set("travelTicket","1");
} else {
return false;
}
}
It could be that one of your if statements that return from the function have a false expression
// Potentially this is the faulty expression
if(money > 1000005 && Cookies.get("modal2alrshown") != "yep"){
....
} else {
// Causing this return to fire, exiting the function
return false;
}
If statement will always work. You need to debug that either money > 1000005 to be true or Cookies.get("modal2alrshown") != "yep" should be true.

Skip Iteration Google Apps Script

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...
}
}

do while doesn't work

I'm building a website but a JavaScript part doesn't work.
Look here the script:
do {
if (percen === 0) {
console.log();
} else if (percen === 1) {
document.getElementById("percen").innerHTML = "Text"
} else if (percen === 2) {
document.getElementById("percen").innerHTML = "Text"
} else if (clicks === 3) {
++percen;
} else if (clicks === 4) {
++percen;
} else if (clicks === 5) {
++percen;
} else if (clicks === 6) {
++percen;
} else if (clicks === 7) {
var percen = 0;
}
}
But when I run it in a HTML file it will not loop. The var "percen" will ++ when you use a button.
Try adding the 'while' portion of the 'do while' loop at the end:
do {
if (percen === 0) {
console.log();
}
// rest of your code
// ...
}
while (percen < 100);
You forgot the condition at the end.. It is while(condition) remember that the condition is checked at the end of the script so it will enter 1 time. If you want you can do a while syntax while(cond){yourscript}

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.

JS loop only works when matching to the last element in an array

I cannot get the loop to work in my simple js login script. When i try to login with any login other than the last one in the array (user3 and pass3) it returns false.
What am I doing wrong?
I have tried both == and ===.
var userLogins = [{user:"user1", password:"pass1"},{user:"user2", password:"pass2"},{user:"user3", password:"pass3"}]
var success = null;
function logon(user, pass) {
userok = false;
for (i = 0; i < userLogins.length; i++)
{
if(pass == userLogins[i].password && user == userLogins[i].user )
{
success = true;
}
else
{
success = false;
}
}
secret(success);
}
function getData() {
var user = document.getElementById("userid").value;
var password = document.getElementById("password").value;
logon(user, password);
}
function secret(auth){
if(auth)
{
show('success');
hide('login');
}
else
{
show('error');
hide('login');
}
}
function show(show) {
document.getElementById(show).className = "show";
}
function hide(hide) {
document.getElementById(hide).className = "hide";
}
for (i = 0; i < userLogins.length; i++)
{
if(pass == userLogins[i].password && user == userLogins[i].user )
{
success = true;
}
else
{
success = false;
}
}
You need a break in there, otherwise your true value for success simply gets overwritten with false on the next iteration... except for the last possible credentials, for which there is no "next" iteration.
Once you've done that, you don't actually need the else branch at all:
var success = false;
for (i = 0; i < userLogins.length; i++) {
if (pass == userLogins[i].password && user == userLogins[i].user) {
success = true;
break;
}
}
Use break when you found it. Otherwise the next loop will set success to false.
for (var i = 0; i < userLogins.length; i++)
{
if(pass == userLogins[i].password && user == userLogins[i].user )
{
success = true;
break;
}
else
{
success = false;
}
}
secret(success);

Categories

Resources