Please help me with the code below

Can someone please help me fix this code? It was suppose to display an image at different time of the day based on the day of the week. When i changed the day to monday it will work but it will still work if i change it to tuesday and it’s not even tuesday.

<!DOCTYPE html>
<html>
<head>
</head>

<body>
	<img id="image">
</body>
	<script>
	    var date = new Date();
	    var day = date.getDay();
	    var hour = date.getHours();
	    var minutes = date.getMinutes();
	    var img;

	    if (day = 0 && day < 6) {
	        if (hour = 8) {
	            if (minutes > 23) {
	                img = "image1";
	            } else {
	                img = "image2";
	            }
	        } else if (hour > 8 && hour < 14) {
	            img = "image1";
	        } else if (hour = 2) {
	            if (minutes < 43) {
	                img = "image1";
	            }
	        } else {
	            img = "image2";
	        }
	    } else {
	        img = "image2";
	    }

	    (function() {
	        document.getElementById("image").src = img + ".jpg";
	    })()
	</script>

</html>

Here’s the problem - you are assigning zero to the day variable. It’s not the only place you have that problem in the code either.

Use triple-equals for comparison instead.

Can you show me an example please? i am bad at this.

Sure thing.

When comparing two things to see if they are equal, use ===

For example:

var foo = "foo";
var bar = "bar";
if (foo === bar) {
    console.log("Something is strange with this universe.");
} else {
    console.log("Nothing to see here, move along.");
}

Can you please do it on my code? I tried it and it won’t work.

All i am trying to do is to display an image at a specific time and at a specific day.

Absolutely - the information in post #2 is all you need.

its not working i tried it :frowning:

Please show us what you tried?

<!DOCTYPE html>
<html>
<head>
</head>

<body>
	<img id="image">
</body>
	<script>
	    var date = new Date();
	    var day = date.getDay();
	    var hour = date.getHours();
	    var minutes = date.getMinutes();
	    var img;

	    if (day === 0 && day < 6) {
	        if (hour = 8) {
	            if (minutes > 23) {
	                img = "image1";
	            } else {
	                img = "image2";
	            }
	        } else if (hour > 8 && hour < 14) {
	            img = "image1";
	        } else if (hour = 2) {
	            if (minutes < 43) {
	                img = "image1";
	            }
	        } else {
	            img = "image2";
	        }
	    } else {
	        img = "image2";
	    }
			if (day === 1 && day < 6) {
					if (hour = 8) {
							if (minutes > 23) {
									img = "image11";
							} else {
									img = "image21";
							}
					} else if (hour > 8 && hour < 14) {
							img = "image1";
					} else if (hour = 2) {
							if (minutes < 43) {
									img = "image1";
							}
					} else {
							img = "image2";
					}
			} else {
					img = "image2";
			}
			if (day === 2 && day < 6) {
					if (hour = 8) {
							if (minutes > 23) {
									img = "image3";
							} else {
									img = "image2=4";
							}
					} else if (hour > 8 && hour < 14) {
							img = "image1";
					} else if (hour = 2) {
							if (minutes < 43) {
									img = "image1";
							}
					} else {
							img = "image2";
					}
			} else {
					img = "image2";
			}

	    (function() {
	        document.getElementById("image").src = img + ".jpg";
	    })()
	</script>

</html>

As Paul explained in post #2, in a few places you are assigning when you need to be comparing.
i.e. The = vs. === bit.

Can you please edit the orignal code and test it? It is not working me please

This page should help much more than me posting code. Something as basic as operators is very important for you to understand, not only for JavaScript but for a great many other languages as well.

I am not that good in this please understand

<html>
<body>


</body>
<script>

var date = new Date();
var hour = date.getHours();
var day = date.getDays();

if (hour > 9 && hour <= 12 && day === 1)
	return 'image2.jpg';
else if (hour > 12 && hour <= 3 && day === 1)
	return 'image1.jpg';
else
	return 'image2.jpg';
}

var image = document.getElementById('your-image');
image.src = getImageSourceByDate();
		</script>
</html>

IMHO starting with a simple script at first is a good idea. That’s how I prefer to work up a script. Working - tweak until it breaks - fix - rinse, repeat.

Notice the getImageSourceByDate() and the return lines.

The getImageSourceByDate() line is “calling” a function named “getImageSourceByDate”, but your code (as it is) has no such function.

If you open your browsers dev tools console you may see an “undefined” error message that could help point you to the problem.

Sometimes functions only “do stuff” without returning anything (void) but very often they return something be it an Int, String, Array, etc.

In this case, what you have should work if you wrap the code inside a function. i.e.

function getImageSourceByDate() { // new open curly brace
var date = new Date(); 
. 
. 
. 
else { 
  return 'image2.jpg'; 
}
}  // new closing curly brace

Can you edit my origanl code and add yours to it please please i am really bad at this

While it’s not as easy for you personally, we tend to prefer helping people to understand why the problem is happening. That way you won’t need us to write your code over and over again afterwards.

2 Likes

I am just trying to learn. I tried so many ways to get work and i cant. I am just gonna gave up becuase no one dont wannt help.

That is simply NOT true. You have plenty of evidence of people trying to help you above (and in other posts). BUT, people are prepared to help you to learn, NOT do it for you.

1 Like