Where did I go wrong? | Sololearn: Learn to code for FREE!

+6

Where did I go wrong?

window.onload = function() { setInterval(showTime, 1000); changeBodyColor(); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; } function bbgcolo(){ var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 0){ document.body.style.backgroundColor.innerHTML = "white"; } else { document.body.style.backgroundColor.innerHTML = "black"; } } I want to change the background color of the page but script isn't working.. Problem solved!! Thanks Thanks Thanks if you had intention of helping me.. But for now, I am closing this case.. If you have better ideas, please share!! https://code.sololearn.com/WcIpzGt2vy6V/?ref=app visit this 👆👆👆

4/3/2021 4:56:30 AM

Mohammad Mehedi Hasan

23 Answers

New Answer

+8

This is wrong: document.body.style.backgroundColor.innerHTML = "white"; This is right: document.body.style.backgroundColor = "white"; Similar applies to setting "black" obviously. innerHTML is for HTML elements. It isn't for CSS properties.

+5

To fix everything, you could do this. I made minimal changes so you could learn from them: window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; bbgcolo(); } function bbgcolo() { var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 0){ document.body.style.backgroundColor = "white"; } else { document.body.style.backgroundColor = "black"; } }

+4

Mohammad Mehedi Hasan provide correct url of images

+4

Edited code:- https://code.sololearn.com/WLVTQSp08b9V/?ref=app

+3

https://code.sololearn.com/WNZsEv7ZNU01/?ref=app

+3

Mohammad Mehedi Hasan for increment in i So, i am declare 'i' outside the function

+2

Edited code:- https://code.sololearn.com/WLVTQSp08b9V/?ref=app

+2

Mohammad Mehedi Hasan share you code link

+2

Mohammad Mehedi Hasan i think you want images changed by according time

+1

window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; if(seconds % 2 == 0){ document.body.style.backgroundColor = "white"; } else { document.body.style.backgroundColor = "black"; } var images = ["images.jpeg", "images (1).jpeg", "images (2).jpeg", "images (3).jpeg", "images (4).jpeg"]; for(var i=0; i<images.length; i++) { document.getElementById("imges").src.innerHTML = images[i]; if(i > images.length) { i =0; } } } Why the images aren't appearing?

+1

urls are correct.. I had put one of them inside src tag in html file

+1

https://code.sololearn.com/W00jB0xLzsh0/?ref=app

+1

+1

can you please explain what it does?

+1

Exams why I need to declare the variable i .. outside of the function showTime

+1

Ayush Mishra, did you just copy/paste my answer? It is word for word exactly the same as what I typed.

+1

This adjusts text colour to be the opposite of the background colour. I also adjusted the background to be black when the second is even like you asked. window.onload = function() { setInterval(showTime, 1000); } function showTime() { var d = new Date(); var hours = d.getHours(); var minutes = d.getMinutes(); var seconds = d.getSeconds(); document.getElementById("time").innerHTML = hours + ":" + minutes + ":" + seconds; bbgcolo(); } function bbgcolo() { var s = new Date(); var secs = s.getSeconds(); if(secs % 2 === 1){ document.body.style.backgroundColor = "white"; document.body.style.color = "black"; } else { document.body.style.backgroundColor = "black"; document.body.style.color = "white"; } } Regarding the images problem, you should create a new question to tackle that after this background colour and text colour problem. It is harder to follow discussions about many diverging topics than stick to a single question. Also, share an example on Sololearn's Code Playground. https://code.sololearn.com/W00jB0xLzsh0/?ref=app is a broken link.

+1

Problem solved!! Thanks Thanks Thanks if you had intention of helping me.. But for now, I am closing this case.. If you have better ideas, please share!! https://code.sololearn.com/WcIpzGt2vy6V/?ref=app visit this 👆👆👆

0

You first need to make sure that the text colour of element #time can be visible over a black/white background colour window.onload = function() { setInterval( showTime, 1000 ); } function showTime() { var d = new Date(); document.getElementById("time").innerHTML = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds(); var bgColour = "white"; if( d.getSeconds() % 2 == 0 ) { bgColour = "black"; } document.body.style.backgroundColor = bgColour; }

-3

This is wrong: document.body.style.backgroundColor.innerHTML = "white"; This is right: document.body.style.backgroundColor = "white"; Similar applies to setting "black" obviously. innerHTML is for HTML elements. It isn't for CSS properties.