lundi 19 septembre 2016

Comparing text of two strings, issues with eq

Problem

For the hockey players that are defensemen .player--defenseman, clicking on the "Add to Team" button btn-add multiple times replaces the text of a span after each click, but it should not add that player's name more than once.

Where I'm at

  • I've tried using the following if-statement spanText !== playerName to compare the two strings, but when I console.log the strings I'm getting the default text "Pick a defenseman" and then that player's name, despite that player being selected and his name already being in the span
  • I have a feeling the problem might be the variable spanText or with eq(0), especially with the section that deals with defensemen

scripts.js

function countPlayers(){
        $(".player").click(function(){

            // Select the current player
            var player = $(this);

            // Count number of players of each position that have been clicked
            var pickedF = $(".player--forward.is-selected").length;
            var pickedD = $(".player--defenseman.is-selected").length;
            var pickedG = $(".player--goalie.is-selected").length;

            // Grab the name of the player last clicked
            playerName = player.find(".player__name").text();

            // Literally magic.
            $(".btn--add").unbind("click");

            $(".btn--add").click(function(){

                // Ensures names don't match
                var spanText = $(".player__pick").eq(0).text();

                // Changes the opacity of a picked player to 0.5
                player.addClass("is-selected");

                if (player.hasClass("player--forward")) {
                    if (spanText !== playerName) {
                        $(".player__pick--forward.is-empty").eq(0).html(playerName);
                        $(".player__pick--forward.is-empty").eq(0).removeClass("is-empty");

                        if (pickedF < 2) {
                            pickedF++;
                        }

                        if (pickedF === 2) {
                            $(".player--forward").not(":has(.is-selected)").css("pointer-events", "none");
                            console.log("Locked forwards");
                        } else {
                            $(".player--forward").css("pointer-events", "auto");
                        }
                    }
                }

                // Something is wonky here
                if (player.hasClass("player--defenseman")) {
                    if (spanText !== playerName) {
                        $(".player__pick--defenseman.is-empty").eq(0).html(playerName);
                        $(".player__pick--defenseman.is-empty").eq(0).removeClass("is-empty");

                        console.log(spanText);
                        console.log(playerName);

                        if (pickedD < 3) {
                            pickedD++;
                        }

                        if (pickedD === 3) {
                            $(".player--defenseman").not(":has(.is-selected)").css("pointer-events", "none");
                            console.log("Locked defensemen");
                        } else {
                            $(".player--defenseman").css("pointer-events", "auto");
                        }
                    }
                }

                if (player.hasClass("player--goalie")) {
                    if (spanText !== playerName) {
                        $(".player__pick--goalie.is-empty").eq(0).html(playerName);
                        $(".player__pick--goalie.is-empty").eq(0).removeClass("is-empty");

                        if (pickedG < 1){
                            pickedG++;
                        }

                        if (pickedG === 1) {
                            $(".player--goalie").not(":has(.is-selected)").css("pointer-events", "none");
                            console.log("Locked goalie");
                        } else {
                            $(".player--goalie").css("pointer-events", "auto");
                        }
                    }
                }

                console.log(pickedF, pickedD, pickedG);
            });

            $(".btn--remove").click(function(){
                player.removeClass("is-selected");

                if (player.hasClass("player--forward")) {
                    $(".player__pick--forward").eq(0).html("Pick a Forward");
                    $(".player__pick--forward").eq(0).addClass("is-empty");

                    if (pickedF > 0 && pickedF < 2) {
                        pickedF--;
                    }
                }

                if (player.hasClass("player--defenseman")) {
                    $(".player__pick--defenseman").eq(0).html("Pick a Defenseman");
                    $(".player__pick--defenseman").eq(0).addClass("is-empty");

                    if (pickedD > 0 && pickedD < 3){
                        pickedD--;
                    }
                }

                if (player.hasClass("player--goalie")) {
                    $(".player__pick--goalie").eq(0).html("Pick a Goalie");
                    $(".player__pick--goalie").eq(0).addClass("is-empty");

                    if (pickedG > 0){
                        pickedG--;
                    }
                }

                console.log(pickedF, pickedD, pickedG);
            });
        });
}

Aucun commentaire:

Enregistrer un commentaire