Question

I have an object with a property containing a large string. This property has a value with random numbers generated earlier in the script in the format x , x , x , x ... (isn't and can't be an array because of other needs for the variable within the program) and so on. I am trying to get the sum of these numbers and my first thought was to use parseInt() to do this by splitting them all up then adding them together, but when I do this it only returns the first number. Is this what I should do but I'm just doing it wrong? Or is there another function that would make this easier?

The program is a blackjack game I'm making to see how well i understand everything I am learning.

Here is the function i am trying to make to see if the user busts when taking a hit (not much so far because i can't figure out the parseInt thing)

'

function checkBust() {

    var total = parseInt(user.hand, 10);

}

'

the user object

'

var user = {
hand: dealUser()
};

'

and the functions to set the object property

function randomCard() {

            // random number between 0 and 10
            var j = Math.random() * 10;

            // round that number into a var called card
            var card = Math.round(j);   

            // if card is 0, assign a J Q or K by making a random number again
            if (card === 0) {

                //another random number
                var k = Math.random() * 10; 

                // checks random number and assign J Q or K                     
                if (k <= 4) {
                    card = 'J';
                } else if (k <= 7) {
                    card = 'Q';
                }
                else {
                    card = 'K';
                }
            }

            // value of the function is a single card
            return card;

        }

function dealUser() {

            // empty array to store cards
            var x = [];

            // var to start for loop 
            var i = 0;      

            // start for loop 
            for (i; i < 2; i++) {

                // add a random card to the i^th index of x
                x[i] = randomCard();
            }
            // value for function is array of two cards x[0] , x[1] 
            var cards = x[0] + " , " + x[1]; 
            return cards;

        }
Was it helpful?

Solution

parseInt will stop parsing when it reaches a non numeric character.

parseInt('1234,5678', 10); // => 1234
// since a comma (,) is not a numeric character, everything after is ignored.

You have to split the string into an array of strings using the comma as the delimiter:

'1234,5678'.split(','); // => ['1234', '5678'];

Then parse each element of the array to convert them to numbers and then you can sum them.

Here's how I'd do it:

var nums = "1,2,3,4,5";

var sum = nums.split(',').reduce(function(memo, num) {
  return memo + parseInt(num, 10);
}, 0);

console.log(sum); // => 15

That should work. See jsbin example.

Note the split parameter needs to match the delimiters you use in your string. for this example ',' is appropriate. For your example you might need /\s*,\s*/.

Unrelated

Since you provided an example of code I can see that you're spending a lot of effort attempting to duck punch and transform the values to the types you need instead of exposing the types in an object. Might I suggest:

function Stack(cards) {
  this.cards = cards || [];
}

Stack.prototype.toString = function() {
  return this.cards.join(' , ');
};

Stack.prototype.sum = function() {
  return this.cards.reduce(function(memo, card) {
    return memo + parseInt(card, 10);
  }, 0);
};

function randomCard() {
  return Math.floor(Math.random() * 13) + 1;
}

Stack.dealHand = function() {
  var card1 = randomCard(), card2;
  do { card2 = randomCard(); } while (card1 === card2);
  return new Stack([card1, card2]);
};

// Example
var hand = Stack.dealHand();
console.log(hand + ' = ' + hand.sum()); // => '3 , 11 = 14'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top