Running a function through an if/else statment


#1

Here is my code to give you a basic understanding of what I'm trying to do.

var location = 1
var location = function () {
if(location = 1) {
function areaOne;
}
else if(location = 2) {
function areaTwo;
}
else if(location = 3) {
function areaThree;
}
else {
}
}

while(location = 1) {
function areaOne() {
    var areaOne = true
    var areaOne = prompt("Input 2, 3, or over")
    while(areaOne) {
    switch (areaOne) {
        
        case '1':
        location = 1;
    break;
    
    case '2':
        location = 2;
    break;
    
    case '3':
        location = 3;
    break;
    
    case 'over':
        location = 0;
    break;
    
    default:
        console.log("Try again");
        location = 1;

I understand there are syntax errors and the code isn't even finished, but what I want to know is if it's possible to run a function if it meets the requirements of the if/else statement. I'm going to make 3 area function like the one above and I want it to run the specific one according to the response you give in the prompt. With that you can travel around as if your in an RPG.


#3

To have a function run only when it meets the requirements of an if or and else. You just need to place it inside!

But if you want it to meet the requirements then run it outside, you will need another if somewhat like this:
>>if(A&&B){var function(chablau) {//code block-do this!} }

In this case A & B need to be returned or be set to a certain value from the if and else so that this if will run!


#5

Can you show me an example in code?


#9

Yeah give me a sec, I´m writing an example


#10

Here is a dumb example, it doesn´t necessarily need to be a for, you could do an if inside another if, that would work as well :wink:

var A = false;
var B = false;

for(var i = 0;i<=5;i++)
{
console.log(i);
if(i===5)
{
A=true;
console.log("You a banana!");

  }

}
for(var x =0; x<=2 ;x++)
{
console.log(x);
if(x===2)
{
B=true;
console.log("I´m a banana!");
}
}
if(A===B)
{
console.log("We a banana");
}


#11

So what would the syntax be to run a function instead of just a console.log?


#12

This will enable the function to be used whenever you call it in another place, it being inside another if or inside another function!

var a =true;
var b =true;

if(a===b)
{

banana = function()
{
  prompt("are you getting this?");
};

}

banana();

<<<


#14

Hey dude. How are you today?


#15

How dare me?? How dare you!!!


#16

Thank you I think I got it now.


#17

Hey how are you Soren the Man!!!!!


#18

Soren please write back to me!!!!!!!


#19

Sorennnnnnn pleaseeee


#20

:rage::rage::rage::rage::rage::rage::rage: no


#21

Hey Soren how are you today?


#22

:hamburger::crocodile: