-1

I'm fairly new to JavaScript and jQuery so any other advice you might have please leave in a comment below. I am making a card game to be played on a single screen in javascript.

Inside the div for id playerHand, I use a javascript function to output the hand of the current player's turn. Whenever I output the current player's hand, I get an output in that div like so:

undefined/ 6H / AD / 7H / 5H / 6S / KC / 8C / 10H / AC / 2H / 8S / 2D / 3S

The hand is made of 13 cards(denoted by numbers 2-10 and J, Q, K, A with a letter for hearts, spades, clubs and diamonds). All 13 cards print out to the div correctly but this random undefined thing is unexpected. The JavaScript function used to output this information is below.

JavaScript jQuery function:

function addHand() {
  var htmlString;
  for(i = 0; i < hands[currentPlayersTurn].handArray.length; i++) {
    htmlString += "/  "+ hands[currentPlayersTurn].handArray[i].name +" ";
  }
  $("#playerHand").html(htmlString);
}
  • 1
    _"The JavaScript function used to output this information is below in the complete javascript code but I pulled the specific function so you don't have to search for it."_ You should have posted only a [MCVE] that reproduces the problem. We don't want "complete code", nor a non-self-contained snippet. Must be the sixth time I've had to point that out today; why is this hard? – Lightness Races in Orbit Feb 22 '17 at 19:39
  • 1
    @LightnessRacesinOrbit probably because all 6 times it was to a different user who hadn't been told it yet and/or doesn't care as long as an answer arrives. – Kevin B Feb 22 '17 at 19:42
  • @KevinB: That is right. However the proportion of questions not showing a [MCVE] is going up. Didn't even think that were possible :( Why do people need to be "told it"? Why don't they _look up how the site works before posting?_ Big sigh. – Lightness Races in Orbit Feb 22 '17 at 19:42
  • @LightnessRacesinOrbit There is that better? – xTaylorFerg Feb 22 '17 at 19:59
  • @KevinB I just looked up the .reduce function and sure it looks nifty but it seems to make it hard for me to understand/read but I will keep it in mind. Maybe more exposure would help haha. I am still a newbie but I'll get there! – xTaylorFerg Feb 22 '17 at 20:12
  • 1
    `htmlString = [{name:'foo'},{name:'bar'}].reduce((result, hand) => {result.push(hand.name); return result;},[]).join(' / ');` there, fixed it :p result.push doesn't return the array. I'd suggest sticking with what you know for important stuff though. – Kevin B Feb 22 '17 at 20:14

2 Answers2

3

Look here:

var htmlString;

htmlString's initial "value" is undefined.

Then you stringise it via string concatenation, and keep adding to that initial value. It's neither "random" nor "unexpected"; it's what you programmed into the computer.

You probably meant:

var htmlString = "";

so as to begin with the empty string.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

Your issues is with var htmlString since you do not initialize it. Though even if you set to empty string your output won't be as you expect because you will then just replace undefined/string/string/string with /string/string/string. A better approach would be to push the list of names into an array and then join them on /.

function addHand() {
  var htmlList = [];
  for(var i = 0; i < hands[currentPlayersTurn].handArray.length; i++) {
    htmlList.push(hands[currentPlayersTurn].handArray[i].name);
  }
  $("#playerHand").html(htmlList.join(" / "));
}
scrappedcola
  • 10,423
  • 1
  • 32
  • 43