Issue
I have a Piece of code that's supposed to change the text displayed according to the current day of the week and the time of day.
For some reason the if/else statements I'm using to check variables are altering the day variable. The end value changes from day do day and removing sections of if else statements also change the result.
I plan on embedding this on a WordPress site using the HTML block
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Buttons</h2>
<p id="demo"></p>
<button id="q">Q</button>
<button id="w">W</button>
<button id="e">E</button>
<button id="r">R</button>
<button id="t">T</button>
<button id="y">Y</button>
<div id="myDIVq">
This is my DIVq element.
</div>
<div id="myDIVw" style="display:none">
This is my DIVw element This space will be used for our weekday schedule.
</div>
<div id="myDIVe" style="display:none">
This is my DIVe element This space will be used for our weekend schedule.
</div>
<div id="myDIVr" style="display:none">
This is my DIVr element This space will be used for our presenters page.
</div>
<div id="myDIVt" style="display:none">
This is my DIVt element.
</div>
<div id="myDIVy" style="display:none">
This is my DIVy element.
</div>
<script>
//EFM Page Content management V2.0
//When Corresponding Button is clicked oly the relevent content will be displayed
// using spread syntax to convert collection to array
// forEach is an array method
[...document.getElementsByTagName("button")].forEach(function(item) {
// adding eventListener to the elements
item.addEventListener('click', function() {
// calling the methods
// this.id will be the id of the clicked button
// there is a method in the object by same name, which will be trigger
obj[this.id]();
})
})
var x1 = document.getElementById("myDIVq")
var x2 = document.getElementById("myDIVw")
var x3 = document.getElementById("myDIVe")
var x4 = document.getElementById("myDIVr")
var x5 = document.getElementById("myDIVt")
var x6 = document.getElementById("myDIVy");
var obj = {
q: function() {
HideAll()
x1.style.display = "block"
},
w: function() {
HideAll()
x2.style.display = "block"
},
e: function() {
HideAll()
x3.style.display = "block"
},
r: function() {
HideAll()
x4.style.display = "block"
},
t: function() {
HideAll()
x5.style.display = "block"
},
y: function() {
HideAll()
x6.style.display = "block"
}
}
function HideAll() {
x1.style.display = "none"
x2.style.display = "none"
x3.style.display = "none"
x4.style.display = "none"
x5.style.display = "none"
x6.style.display = "none"
}
//Initailize Date and Time
Time = new Date()
let day = Time.getDay()
let hour = Time.getHours()
//Show is a Test variable
let show;
//Everyday Shows
if (3 <= hour && hour < 4) {
show = "Gospel";
//6 to 9 Show (All Weekdays)
//Sunday Shows
} else if ((day = 0) && (4 <= hour && hour < 5)) {
show = "Gospel";
//6 to 7 Show (Sundays)
} else if ((day = 0) && (5 <= hour && hour < 6)) {
show = "Sermon";
//7 to 8 Show (Sundays)
} else if ((day = 0) && (6 <= hour && hour < 9)) {
show = "Sunday Breakfast with Leonie";
//8 to 11 Show (Sundays)
} else if ((day = 0) && (9 <= hour && hour < 12)) {
show = "Lovers Corner";
//11 to 14 Show (Sundays)
} else if ((day = 0) && (12 <= hour && hour < 15)) {
show = "Country With Dewald";
//6 to 9 Show (Sundays)
} else if ((day = 0) && (15 <= hour && hour < 17)) {
show = "T-Junction with Manie";
//17 to 19 Show (Sundays)
} else if ((day = 0) && (17 <= hour && hour < 20)) {
show = "Jou Keuse with Sol";
//19 to 22 Show (Sundays)
//Weekday Shows
} else if ((1 <= day && day < 6) && (4 <= hour && hour < 7)) {
show = "PS Show";
//6 to 9 Show (All Weekdays)
} else if ((1 <= day && day < 6) && (7 <= hour && hour < 10)) {
show = "Daylight With Dewald";
//9 to 12 Show (All Weekdays)
} else if ((1 <= day && day < 6) && (10 <= hour && hour < 13)) {
show = "Weekly View";
//12 to 15 Show (All Weekdays)
} else if ((1 <= day && day < 6) && (13 <= hour && hour < 16)) {
show = "Sunset Drive";
//15 to 18 Show (All Weekdays)
} else if ((day = 3) && (16 <= hour && hour < 17)) {
show = "Live Well";
//18 to 19 Show (Wednesdays)
} else if ((day = 4) && (16 <= hour && hour < 17)) {
show = "Trends";
//18 to 19 Show (Thursdays)
} else if ((1 <= day && day <= 4) && (17 <= hour && hour < 20)) {
show = "Jono Mich Monday to Thursday";
//19 to 22 Show (Monday to Thursday)
} else if ((day = 5) && (17 <= hour && hour < 20)) {
show = "Fryday Jol";
//19 to 22 Show (Friday)
//Saturday Shows
} else if ((6 <= day) && (4 <= hour && hour < 7)) {
show = "Weekend Buzz with Sam";
//6 to 9 Show (Saterday)
} else if ((6 <= day) && (7 <= hour && hour < 10)) {
show = "Saterday Shakeup with Sol";
//9 to 12 Show (Saterday)
} else if ((6 <= day) && (10 <= hour && hour < 14)) {
show = "Top 40 With Thabiso";
//12 to 16 Show (Saterday)
} else if ((6 <= day) && (14 <= hour && hour < 16)) {
show = "Drive Time With Laurette";
//16 to 18 Show (Saterday)
} else if ((6 <= day) && (16 <= hour && hour <= 19)) {
show = "Rock show With Kelly";
//18 to 21 Show (Saterday)
//If there are now shows
} else {
show = "Music";
}
//Changes demo div to display active show, test
document.getElementById("myDIVq").innerHTML = show;
document.getElementById("myDIVy").innerHTML = day + " " + hour;
//if (500 < size && size < 600) { doStuff(); }
</script>
</body>
</html>
Solution
This is happening because you are assigning the value in the if check. instead of assigning it using =
, use ==
or ===
to check for equality
if (day = 0) // incorrect
if (day == 0) // correct
Answered By - Cool guy
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.