-3

Essentially, I want to have it so that

if(condition) {"do this" || "do that"};

Specifically, I'm having it so that if a specific div is set to a specific color (picked randomly from an array), then 1 of 4 other divs changes its color to a specific color.

Thanks!

EDIT: I guess I'm more wondering if I can randomize a 'then' statement. I'm making a game, so I want to avoid choosing which of the 4 divs I want to obtain the new color (which would mean every instance is scripted the same each time)

pacduck
  • 11
  • 4
  • what is `||` actually meaning in this context? – Nina Scholz Apr 24 '17 at 14:22
  • You can use if / else in the if statement – Cyril Beeckman Apr 24 '17 at 14:22
  • So, what does that pseudo code mean: "if condition then do this or do that"? How would the computer know when it has to do "this" and when it has to do "that"? Solution to your problem: Use multiple if-statements, one for each specific condition, or use an else-statement. – Jesper Apr 24 '17 at 14:23
  • 1
    What I would recommend, instead of breaking the OP's spirit to try and learn to code, is look up on what you want to do first. https://www.w3schools.com/php/default.asp is the first place you would like to search as it has everything you wil ever need. If nothing, then you google it to see if anybody else had the same problem and if still nothing then you ask it on stackoverflow. There are rules for programming and the biggest mistake is to **not research first** – CAllen Apr 24 '17 at 14:32
  • @CAllen I have done research and google searched and all of that. My searches weren't fruitful and it might just be that I'm wording my problem poorly. But thank you. – pacduck Apr 24 '17 at 14:34

2 Answers2

1

There can be many executions for an if statement. As much as you like. What you can do is use multiple if in that one if statement to select the proper div or use a switch instead. For example:

var array = [3, 4, 1, 2];

NOTE sometime what I do is shuffle the array, which mixes up the indexes, before randomly picking

var my_array = array.sort(); // This will change your array, for example, from [3, 4, 1, 2] to [1, 2, 3, 4].
or 
var my_array = array.reverse(); // This will change your array, for example, from [3, 4, 1, 2] to [4, 3, 2, 1].

var random_condition = Math.floor((Math.random() * 3)); // select at random from 0 to 3 because the array is ZERO based

And then you do your logc:

if(condition) {
    if (random_condition == 1 ) {
        "do this" with div 1 // array [0] == 1
    }
    else if (random_condition == 2 ) {
        "do this" with div 2 // array [1] == 2
    }
    else if (random_condition == 3 ) {
        "do that" with div 3 // array [2] == 3
    }
    else if (random_condition == 4 ) {
        "do that" with div 4 // array [3] == 4
    }
}

Or use a switch

if(condition) {
    switch (random_condition) {
        CASE '1':
            "do this" with div 1 // array [0] == 1
            break;
        CASE '2':
            "do this" with div 2 // array [1] == 2
            break;
        CASE '3':
            "do this" with div 3 // array [2] == 3
            break;
        CASE '':
            "do this" with div 4 // array [3] == 4
            break;
        default
            // do nothing
            break;
    }
}
CAllen
  • 856
  • 5
  • 14
  • I guess I'm more wondering if I can randomize a 'then' statement. I'm making a game, so I want to avoid choosing which of the 4 divs I want to obtain the new color (which would mean every instance is scripted the same each time) – pacduck Apr 24 '17 at 14:35
  • I did an update to the anser using the randomly picked array – CAllen Apr 24 '17 at 14:35
  • Thank you, this seems to be what I need! Once I get the chance to implement it, I'll post back! – pacduck Apr 24 '17 at 14:48
  • Your welcome :) Good luck and have fun with your project :) – CAllen Apr 24 '17 at 15:44
  • Hey I am sorry I gave you an answer in php. I honestly did not see the `javascript` tag but it is literally the same logic..I will update the answer to match using javascript instead. But the conditions and switch remains the same. The only thing dat change will be the random and the sellection from the array – CAllen Apr 24 '17 at 15:49
0

it is possible to do several things in one block (surrounded by { and }), but simply

if(condition) {
 console.log("either do this"); 
 console.log("and do that");
} else {
 console.log("or do this"); 
 console.log("and this as well");
}

the or '||' like in e.g. shell scripts is not used here in javascript.

the else part can be split again e.g.

if (c1) {
} elseif (c2) {
} else {
}

and this elseif you may repeat for as many conditions you like.

you could also dice:

function dice() {
    return Math.floor(Math.random() * 6 + 1);
}

and then just do sth to an element that has the correct number:

getElementById("div"+dice()).innerHtml = "changed";
vv01f
  • 362
  • 1
  • 4
  • 21
  • I guess I'm more wondering if I can randomize a 'then' statement. I'm making a game, so I want to avoid choosing which of the 4 divs I want to obtain the new color (which would mean every instance is scripted the same each time) – pacduck Apr 24 '17 at 14:30
  • look for `random` (e.g. https://www.w3schools.com/jsref/jsref_random.asp) and then have either different tasks or just name the targets with possible outcomes of your random function. – vv01f Apr 24 '17 at 14:36