0

This jQuery works on 3 divs - leftKey, rightKey and mentors. They are all aligned properly. The goal is that when the leftKey is clicked, mentors will cycle through a list of background colors. I defined the colors in an array; red then blue then green. I've gotten the keys to respond on click, but the switch doesn't work.

$(document).ready(function() {
          var colors = [ "rgb(128, 0, 0)", "rgb(255, 0, 0)", "rgb(0, 0, 255)", "rgb(0, 128, 0)"];
          //burgundy, red, blue, green
          var mentors = $("#mentors");
          $("#leftKey").click(function() {
            if(mentors.css("background-color") == colors[0]) {
              mentors.css("background-color", colors[colors.length-1]);
            } else {
              for(var x = 0; x < colors.length; x++) {
                if( mentors.css("background-color") == colors[x]) {
                  mentors.css("background-color", colors[x-1]);
                }
              }
            };
          });
          $("#rightKey").click(function() {
            if( mentors.css("background-color") == colors[colors.length-1]){
              mentors.css("background-color", colors[0]);
            } else {
              for(var x = 0; x < colors.length; x++) {
                if( mentors.css("background-color") == colors[x] ) {
                  mentors.css("background-color", colors[x+1]);
                  return false;
                }
              }
            };
          });
4
  • 2
    It seems you're missing a closing parentheses after switch($("#mentors").css("background-color") Commented Feb 19, 2014 at 20:19
  • Also missing a closing parentheses after in case colors[0]: $("#mentors").css("background-color", colors[1]; of css() method Commented Feb 19, 2014 at 20:22
  • Alright. I think my problems are just caused by missing parentheses. What is the best program for finding & matching parentheses? I'm just using TextEdit on my mac Commented Feb 19, 2014 at 20:29
  • 3
    There's a list of good code editors for OS X: stackoverflow.com/questions/20533/mac-text-code-editor Commented Feb 19, 2014 at 20:32

2 Answers 2

1

To simplify your life quite a bit, some refactoring is in order. Try this:

var colors = [ "red", "blue","green"],
    getColor = function (leftRight, currentColor) {
        var newColorIndex
            isLeft = leftRight === "left";
        if (currentColor === colors[0]) {
            newColorIndex = (isLeft) ? 2 : 1;
        } else if (currentColor === colors[1]) {
            newColorIndex = (isLeft) ? 0 : 2;
        } else {
            newColorIndex = (isLeft) ? 1 : 0;
        }
        return colors[newColorIndex];
    },
    colorSwitch = function (leftRight) {
        var mentors = $("#mentors"),
            currentColor = mentors.css("background-color"),
            newColor = getColor(leftRight, currentColor);
        $("#mentors").css("background-color", newColor);
    };
$(document).ready(function() {
    $("#leftKey").click(function() {
        colorSwitch("left");
    });
    $("#rightKey").click(function() {
        colorSwitch("right");
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can't do it that way because $("#mentors").css("background-color"); returns the color in rgb, ex. rgb(255, 0, 0)

You can achieve it through incrementing and decrementing an index. The benefit to doing it this way is that you can have as many colors as you want in the colors array without having to add another case in a switch statement:

$(document).ready(function() {
    var index = 0;
    var colors = [ "red", "blue", "green" ];
    $("#mentors").css("background-color", colors[index]);
    $("#leftKey").click(function() {
        index -= 1;
        if (index < 0)
            index = colors.length - 1;
        $("#mentors").css("background-color", colors[index]);
    });
    $("#rightKey").click(function() {
        index += 1;
        if (index >= colors.length)
            index = 0;
        $("#mentors").css("background-color", colors[index]);
    });
});

DEMO: http://jsfiddle.net/w3h46/4/

2 Comments

What is the advantage of the index as a separate variable? I used a for loop, but when it reaches the end of the array it doesn't loop properly. e.g. clicking right key will go from red to blue, blue to green but not green to red. for(x < colors.length) if mentors = x color = x+1
that way you can increment and decrement the index to get the left or right colors. you can add as many colors as you want in the color array without having to add another "case" to the "switch" statement. also you can repeat colors in the array if you need to do that, ex: ["red", "blue", "red", "green"], and you won't run into problems with duplicate color names. you can't do it the way you originally were because if you do alert($("#mentors").css("background-color"); in your original code you'll see that the output is in "rgb(###, ###, ###)" format. therefore, "rgb(255, 0, 0)" != "red".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.