reset password
Author Message
smahbub2
Posts: 12
Posted 14:52 May 24, 2015 |

Hey, can anyone help me with my code for the second part of the lab. I understand that it has to be an if/else statement but nothing I do is working. I'm going to simplify some of my code a bit because I'm not sure if directly posting it up is allowed.
When I click the button the first time, the page changed colors, but when I click it again, it doesn't. Any help would be appreciated :)


<body>
<button
onclick="backgroundColor()">
Change the background color!</button> 
</body>

<script>
function backgroundColor() {


    if(("body"backgroundColor = "1") &&
        ("div"backgroundColor= "2"))
        {
        "body"backgroundColor = 3;
        "div"backgroundColor= 4;
        }

    else if (("body"backgroundColor = 3) &&
        ("div"backgroundColor= 4))
        {
        "body"backgroundColor = "1";
        "div"backgroundColor= "2";
        }

    else
        {
        "body"backgroundColor = "1";
        "div"backgroundColor= "2";
        }    
}

</script>
-->I also tried changing all the "backgroundColor" to "bgColor" but then the page doesn't change colors at all.

Last edited by smahbub2 at 14:53 May 24, 2015.
cesar714
Posts: 30
Posted 15:22 May 24, 2015 |

I tried using your code but nothing happens I don't understand why you are using "body"backgroundColor = "1". In order to change the color of the background you use document.bgColor = "(your color goes here)";

The structure of your function should be a follows

<script>
    function backgroundColor()
    {
        // create a variable
        if(/*variable is true*/)
        {
            document.bgColor = /*(your color goes here)*/;
        }
        else
        {
            document.bgColor = /*(your new color goes here)*/;
        }

    }
</script>

Now to get the paragraph text to change color you use the document function such as the getElementByID. To give you a clue for your variable being true/false think of it mathematically.

Example:

variable = 1 is true

variable = 0 is false

Hope this helps

smahbub2
Posts: 12
Posted 15:39 May 24, 2015 |

I guess my "simplified" version was bad, my bad. My actual code is this:

 

<script>

function backgroundColor() {


    if((document.getElementById("body").style.backgroundColor = "blue") &&
        (document.getElementById("div").style.backgroundColor= "red"))
        {
        document.getElementById("body").style.backgroundColor = "brown";
        document.getElementById("div").style.backgroundColor= "yellow";
        }

    else if ((document.getElementById("body").style.backgroundColor = "brown") &&
        (document.getElementById("div").style.backgroundColor= "yellow"))
        {
        document.getElementById("body").style.backgroundColor = "blue";
        document.getElementById("div").style.backgroundColor= "red";
        }

    else
        {
        document.getElementById("body").style.backgroundColor = "blue";
        document.getElementById("div").style.backgroundColor= "red";
        }    
} 

     </script>

 

Sorry for the confusion

Last edited by smahbub2 at 15:41 May 24, 2015.
cesar714
Posts: 30
Posted 16:27 May 24, 2015 |

Okay this is what i think is your issue however I am not sure. The issue with your function is that in the if statement you are stating that if the body and div tags are a certain color than change them to another color. To my knowledge with if statements is that in the condition you compare

For instance,

var number = 5;

if(number == 5) {document.getElementByID("style").style.backgroundColor = "red";}

This is how I would approach the condition.

One major difference between = and == is that one is assigning and the other is comparing

For example,

var number = 5; the variable number is now assigned 5, meaning it has a value of 5

if (number == 5) {} the variable is being compared to 5 

if(5 == 5 ) {} true

In your code in the if statement your are assigning the id element to blue and div element to red. Not necessarily a condition.

I tried approaching it with your condition but I did not get anywhere. I hope this helps and good luck.

cesar714
Posts: 30
Posted 16:37 May 24, 2015 |

Okay never mind leave your code AS IS

Inside your if statement add == instead of = this should fix your issue

cesar714
Posts: 30
Posted 16:39 May 24, 2015 |

It should fix your issue assuming your default colors are blue for body and red for div

otherwise change it to this and it should work

if((document.getElementById("body").style.backgroundColor == "white") &&
        (document.getElementById("div").style.backgroundColor == "black"))

smahbub2
Posts: 12
Posted 12:55 May 25, 2015 |

Thanks Cesar, I changed my code and it works now :)

cesar714
Posts: 30
Posted 15:19 May 25, 2015 |

No problem. Glad to help. 

Also, in reference to your CS122 forum an alternative to logging in to MySQL is as follows:

By using your spotlight you can access MySQL server. it will open the MySQL server on the system preferences. Here you can start/stop the server without having to enter the sudo command. After that everything else is the same as NBTANG stated.

Goodluck