Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    splice method not working with an array inside an object

    My goal is to create a deck of flash cards. I want the user to define the highest number that could be displayed as a factor on any card. Then I want to pull a random card using the splice method on the array inside the Deck.

    My problem is that the splice method doesn't seem to work and I can't figure out what I'm doing wrong. Any help would be appreciated.

    Here's the code:

    Code:
    function Card(f1,f2){
    var num1=f1;
    var num2=f2;
    this.getFactor1=function(){
    	return num1;
    	}
    this.getFactor2=function(){
    	return num2;
    	}
    this.getProduct=function(){
    	return num1*num2;
    	}
    }
    
    function Deck(userInput){
    var max=userInput;
    var cards=[];
    for(i=0;i<=max;i++){
    	for(j=0;j<=max;j++){
    		cards[cards.length]=new Card(i,j);
    	}
    }
    this.getCard=function(){
    var len=cards.length;
    var rand=Math.floor(Math.random()*len);
    if(len===0){
    	return "Deck Empty!"
    	}
    var b=cards.splice(rand, 1); //this doesnt work
    return b.getProduct(); //returns nothing
    }
    
    }
    
    function startGame(){
    var num=document.getElementById('limit').value;
    var deck=new Deck(num);
    alert(deck.getCard());
    
    }
    Last edited by Vexed; 01-26-2013 at 10:33 PM.

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    You don't need to rename/store every parameter you pass. Closures will keep your variable alive. Furthermore, you should use a 'var' for the 'i' and 'j' in your loop too to keep it a local variable. After the "Deck empty" result you're missing a semicolon – it's optional, but highly recommended. Lastly, you should move the check for len === 0 to right after reading the len. No need to do all the other stuff if you're not gonna use it anyway.

    To get to the actual question: splice is returning an array, even if it contains only one element. The array doesn't know the method "getProduct", but it's element does.

    Code:
    function Card(num1, num2) {
        this.getFactor1 = function () {
            return num1;
        }
        this.getFactor2 = function () {
            return num2;
        }
        this.getProduct = function () {
            return num1 * num2;
        }
    }
    
    function Deck(max) {
        var cards = [];
        for (var i = 0; i <= max; i++) {
            for (var j = 0; j <= max; j++) {
                cards[cards.length] = new Card(i, j);
            }
        }
        
        this.getCard = function () {
            var len = cards.length;
            if (len === 0) {
                return "Deck Empty!";
            }
    
            var rand = Math.floor(Math.random() * len);
            var b = cards.splice(rand, 1)[0];
    
            return b.getProduct();
        }
    
    }
    
    function startGame() {
        var num = document.getElementById('limit').value;
        var deck = new Deck(num);
        alert(deck.getCard());
    
    }
    Last edited by Airblader; 01-26-2013 at 08:57 PM.

  • Users who have thanked Airblader for this post:

    Vexed (01-26-2013)

  • #3
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks Airblader!

    I really appreciate your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •