i'm trying to throw together a simple javascript page with multiple if statements. the idea is to append to a list based on the evaluation of a stack of if statements. the problem is if any one of them fails, it triggers the else statement. i only want it to trigger else in the case that all of them fail.
<p id="fruit">My fruit basket has: </p>
if (apples) {
document.getElementById("fruit").innerHTML += "apples";
}
if (oranges) {
document.getElementById("fruit").innerHTML += "oranges";
}
if (bananas) {
document.getElementById("fruit").innerHTML += "bananas";
}
else {
document.getElementById("fruit").innerHTML += "nothing";
}
when i run this and all conditions are met, i'll get:
My fruit basket has: apples oranges bananas
when i run this and no conditions are met, i'll get:
My fruit basket has: nothing
but if any one of the conditions is not met, i'll get:
My fruit basket has: apples nothing
or
My fruit basket has: apples bananas nothing
i've tried using if else, but then it quits after the first 'match'. so in the above example if i have both apples and bananas, using if else will return
My fruit basket has: apples
and not tell me i also have bananas.
question: how do i do this so the else statement is only run in the case that all if statements have failed?
EDIT i tried to make this as simple as i could, but it seems i over simplified it.
the else nothing isn't known until all the if statements have been run. that's literally it's condition: if all the ifs fail.
i tried using the OR operator and it just broke everything. in my code, what's actually happening at if (apples) is it's checking today's date to see if it falls within a range of dates. it needs to do this for a whole bunch of different fruit based on a schedule that is pretty complicated, i.e. figuring out exactly which fruit are in the basket on a given day is harder than just running the schedule against these if 'rules'.
var isSunday = new Date().getDay();
var apples1Start = new Date(2018,8,4); // sept 4, 0 == jan.
var apples1Stop = new Date(2018,8,23); // sept 22, stop dates need to be day after.
var apples2Start = new Date(2018,10,5); // nov 5
var apples2Stop = new Date(2018,10,25); // nov 24
var apples3Start = new Date(2018,10,26); // nov 26
var apples3Stop = new Date(2018,11,9); // dec 8
var oranges1Start = new Date(2018,3,30); // april 30
var oranges1Stop = new Date(2018,4,27); // may 26
var oranges2Start = new Date(2018,9,22); // oct 22
var oranges2Stop = new Date(2018,10,4); // nov 3
var oranges3Start = new Date(2018,3,28); // april 28
var oranges3Stop = new Date(2018,3,29); // april 28
var bananas1Start = new Date(2018,0,1); // jan 1
var bananas1Stop = new Date(2018,3,10); // april 9
var bananas2Start = new Date(2018,6,2); // july 2
var bananas2Stop = new Date(2019,3,11); // april 10
var kiwi1Start = new Date(2018,9,13); // oct 13
var kiwi1Stop = new Date(2018,10,25); // nov 24
var papaya1Start = new Date(2018,8,29); // sept 29
var papaya1Stop = new Date(2018,8,30); // sept 29
var papaya2Start = new Date(2018,10,1); // nov 1
var papaya2Stop = new Date(2018,10,4); // nov 3
var papaya3Start = new Date(2018,9,15); // oct 15
var papaya3Stop = new Date(2018,10,25); // nov 24
var papaya4Start = new Date(2018,10,26); // nov 26
var papaya4Stop = new Date(2018,11,9); // dec 8
var papaya5Start = new Date(2018,11,10); // dec 10
var papaya5Stop = new Date(2019,1,1); // dec 31
var clemantines1Start = new Date(2018,0,1); // jan 1
var clemantines1Stop = new Date(2018,2,1); // feb 28
var clemantines2Start = new Date(2018,9,13); // oct 13
var clemantines2Stop = new Date(2019,1,29); // feb 28
var pears1Start = new Date(2018,8,10); // sept 10
var pears1Stop = new Date(2019,0,3); // jan 2
var tangerines1Start = new Date(2018,0,1); // jan 1
var tangerines1Stop = new Date(2018,2,9); // mar 8
var tangerines2Start = new Date(2018,11,20); // dec 20
var tangerines2Stop = new Date(2019,2,9); // mar 8
var starfruit1Start = new Date(2018,0,1); // jan 1
var starfruit1Stop = new Date(2018,2,9); // mar 8
var starfruit2Start = new Date(2018,9,13); // oct 12
var starfruit2Stop = new Date(2019,2,9); // mar 8
var lemons1Start = new Date(2018,0,1); // jan 1
var lemons1Stop = new Date(2018,2,1); // feb 28
var lemons2Start = new Date(2018,10,1); // nov 1
var lemons2Stop = new Date(2019,2,1); // feb 28
var apricots1Start = new Date(2018,0,1); // jan 1
var apricots1Stop = new Date(2018,1,1); // jan 31
var apricots2Start = new Date(2018,9,1); // oct 1
var apricots2Stop = new Date(2019,1,1); // jan 31
var tomatoes1Start = new Date(2018,0,1); // jan 1
var tomatoes1Stop = new Date(2018,1,1); // jan 31
var tomatoes2Start = new Date(2018,9,1); // oct 1
var tomatoes2Stop = new Date(2019,1,1); // jan 31
var today = new Date();
function yarp() {
document.body.style.background = "green";
document.getElementById("main").innerHTML = "you have fruit";
}
function narp() {
document.body.style.background = "#ffffff";
document.getElementById("main").innerHTML = "nothing";
}
if (isSunday == 0) {
document.body.style.background = "#ffffff";
document.getElementById("main").innerHTML = "sunday";
document.getElementById("fruit").innerHTML = "no fruit on a sunday.";
}
else {
if (today >= apples1Start && today <= apples1Stop || today >= apples2Start && today <= apples2Stop || today >= apples3Start && today <= apples3Stop) {
yarp();
document.getElementById("fruit").innerHTML += "apples! ";
}
if (today >= oranges1Start && today <= oranges1Stop || today >= oranges2Start && today <= oranges2Stop || today >= oranges3Start && today <= oranges3Stop) {
yarp();
document.getElementById("fruit").innerHTML += "oranges. ";
}
if (today >= bananas1Start && today <= bananas1Stop || today >= bananas2Start && today <= bananas2Stop) {
yarp();
document.getElementById("fruit").innerHTML += "bananas. ";
}
if (today >= kiwi1Start && today <= kiwi1Stop) {
yarp();
document.getElementById("fruit").innerHTML += "kiwi. fancy! ";
}
if (today >= papaya1Start && today <= papaya1Stop || today >= papaya2Start && today <= papaya2Stop || today >= papaya3Start && today <= papaya3Stop || today >= papaya4Start && today <= papaya4Stop || today >= papaya5Start && today <= papaya5Stop) {
yarp();
document.getElementById("fruit").innerHTML += "papaya. ";
}
if (today >= clemantines1Start && today <= clemantines1Stop || today >= clemantines2Start && today <= clemantines2Stop) {
yarp();
document.getElementById("fruit").innerHTML += "clemantines. ";
}
if (today >= pears1Start && today <= pears1Stop) {
yarp();
document.getElementById("fruit").innerHTML += "pears. ";
}
if (today >= tangerines1Start && today <= tangerines1Stop || today >= tangerines2Start && today <= tangerines2Stop ) {
yarp();
document.getElementById("fruit").innerHTML += "tangerines. ";
}
if (today >= starfruit1Start && today <= starfruit1Stop || today >= starfruit2Start && today <= starfruit2Stop ) {
yarp();
document.getElementById("fruit").innerHTML += "star fruit. ";
}
if (today >= lemons1Start && today <= lemons1Stop || today >= lemons2Start && today <= lemons2Stop ) {
yarp();
document.getElementById("fruit").innerHTML += "lemons. ";
}
if (today >= apricots1Start && today <= apricots1Stop || today >= apricots2Start && today <= apricots2Stop ) {
yarp();
document.getElementById("fruit").innerHTML += "apricots. ";
}
if (today >= tomatoes1Start && today <= tomatoes1Stop || today >= tomatoes2Start && today <= tomatoes2Stop ) {
yarp();
document.getElementById("fruit").innerHTML += "tomatoes. ";
}
else {
narp();
}
}
#horizon {
position: absolute;
top: 50%;
width: 100%;
}
#content {
position: absolute;
top: -63px;
text-align: center;
width: 100%;
}
#main {
line-height: 75px;
font-size: 100px;
margin: 0;
}
<div id="horizon">
<div id="content">
<h1 id="main"></h1>
<p id="fruit"></p>
</div>
</div>
ifstatements with noelsebranches.