0

I need to random objects on array to "make a battle". The random works perfect, but sometimes repeat objects. Also, I can see my var ARR it's full of undefined. I don't understand how I need to do to random perfectly without repeated object and fill with randomed objects.

var avenger = [
        {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10},
        {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15},
        {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13},
        {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20},
        {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8},
        {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14},
        {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5},
        {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18}
    ]

    function avengerPairs(myObject) {

        var arr = [];

        for (var i=0; i<avenger.length; i++) {

            var randomAvenger = avenger[Math.floor(Math.random() * avenger.length)];

            if (randomAvenger[i] !== avenger[i]) {
                arr.push([randomAvenger, avenger[i+1]]);
                i++;    
            }
        }

        console.log(arr);

        for (var i=0; i<arr.length; i++) {

            console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv));

            if (arr[i][0].markAv < arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!");
            } else if (arr[i][0].markAv === arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!");
            } else {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!");
            }
        }
    }

avengerPairs(avenger);
1
  • Hi there -- are you trying to push random avenger properties into the arr array or full avenger objects. (example property: fullName: "Tony Stark") Commented Apr 25, 2018 at 21:55

3 Answers 3

3

Your array is being filled with undefined because of these lines:

var arr = [];

for (var i=0; i<avenger.length; i++) {

    var random = avenger[Math.floor(Math.random() * avenger.length)];

    console.log(random);
    arr.push([random[i], random[i+1]]);
    i++;
}

random successfully creates a random index to look into the avenger array and assigns the corresponding value in the array to the variable random.

You should see this successfully in the following console.log.

It's the next line that gives you trouble. Inside your push call, you try to access two indices on the random variable with random[i] and random[i + 1]. This returns undefined, because your random variable is not an array of avengers but a single avenger, and the integer keys don't exist on that object, thus returning undefined.

I would recommend a better name for your random variable. Perhaps randomAvenger? That would help make it clear that you're incorrectly indexing into an avenger, instead of an array of avengers.

To successfully pair two avengers, you will need two separate random indexes into the avengers array. You'll also want to watch out for selecting the same avenger twice though!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Alex, I will try to figure it.
I tried to fix that. Now it's perfect. Random works perfectly, but I can't do the random without repeated objects on the array @Alex
1

So I believe the problem is at arr.push([random[i], random[i+1]]).

If you look at the console.log(random) output, you'll realize that random represents an Object of a single avenger (i.e. { id: 4, fullName: 'Bruce Banner', avengerName: 'Hulk', gender: 'Male', city: 'Maryland',markAv: 20 }).

That means random[i] and random[i+1] are both undefined.

My suggestion is to do arr.push([random, avenger[i+1]]);.

You could also use Array.prototype.splice to remove each avenger as you pair them up, so there are no duplicates.

Okay a few hours later and I went ahead and got it to work with splicing / without repeats. I left in a bunch of commented out console.log statements if you want to follow along.

var avenger = [
        {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10},
        {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15},
        {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13},
        {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20},
        {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8},
        {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14},
        {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5},
        {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18}
    ]

    function avengerPairs(myObject) {

        var arr = [];
        var lengthSave = avenger.length

        for (var i=0; i<lengthSave; i++) {
            var newLength = avenger.length
            var index = Math.floor(Math.random() * newLength)
            var randomAvenger = avenger[index];
            var pairArr = (avenger.splice(index, 2)) 

            // console.log(avenger.length)
            // console.log(pairArr.length)

            if (pairArr.length < 2 ) {
                var anotherPair
                if (avenger.length > 1) {
                    anotherPair = avenger.splice(index, 1)[0]
                } else if (avenger.length === 1) {
                   anotherPair = avenger.splice(0, 1)[0]
                } 
                // console.log(!!anotherPair)
                // console.log(anotherPair)
                // console.log('another pair')

                if (!!anotherPair === true) {
                    pairArr.push(anotherPair)
                }
            }

            // console.log(pairArr)
            // console.log(!!pairArr[0])
            // console.log(!!pairArr[1])

            // console.log('pairArr')

            if (!!pairArr[0] === true && !!pairArr[1]) {
                arr.push(pairArr)   
            }


        }

        // console.log(arr);

        for (var i=0; i<arr.length; i++) {

            // console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv));

            if (arr[i][0].markAv < arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!");
            } else if (arr[i][0].markAv === arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!");
            } else {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!");
            }
        }
    }

avengerPairs(avenger);

2 Comments

Thanks @LShapz, I will try to find the solution. I don't understand some things but surely finally I will do!
Hello @LShapz. I tried your code and works perfectly but now I can't make the random without repeated objects.
0

Random works perfectly, but I can't random avenger with randomAvenger to prevent repeated values. Now, between two values into objects in array [a,b] [a,b] it's perfect, but between values to objects, continues repeating values.

That's my code new code:

var avenger = [
        {id: 1, fullName: "Steve Rogers", avengerName: "Captain America", gender: "Male", city: "New York City", markAv: 10},
        {id: 2, fullName: "Tony Stark", avengerName: "IronMan", gender: "Male", city: "New York City", markAv: 15},
        {id: 3, fullName: "Thor Odinson", avengerName: "Thor", gender: "Male", city: "Los Angeles", markAv: 13},
        {id: 4, fullName: "Bruce Banner", avengerName: "Hulk", gender: "Male", city: "Maryland", markAv: 20},
        {id: 5, fullName: "Clint Barton", avengerName: "Hawkeye", gender: "Male", city: "Los Angeles", markAv: 8},
        {id: 6, fullName: "Natasha Romanoff", avengerName: "Black Widow", gender: "Female", city: "Paris", markAv: 14},
        {id: 7, fullName: "Nick Fury", avengerName: "Nick Fury", gender: "Female", city: "New York City", markAv: 5},
        {id: 8, fullName: "Jaume Serradell", avengerName: "Jaumeserr", gender: "Male", city: "Barcelona", markAv: 18}
    ]

    function avengerPairs(myObject) {

        var arr = [];

        for (var i=0; i<avenger.length; i++) {

            var randomAvenger = avenger[Math.floor(Math.random() * avenger.length)];


            if (randomAvenger !== avenger[i]) {
                arr.push([randomAvenger, avenger[i+1]]);
                i++;    
            } else {
                console.log("Not equals");
            }   
        }

        console.log(arr);

        /*
        for (var i=0; i<arr.length; i++) {

            console.log(Math.max(arr[i][0].markAv, arr[i][1].markAv));

            if (arr[i][0].markAv < arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][1].fullName + " is better!");
            } else if (arr[i][0].markAv === arr[i][1].markAv) {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => Are equals!");
            } else {
                console.log(arr[i][0].fullName + " vs " + arr[i][1].fullName + " => " + arr[i][0].fullName + " is better!");
            }
        }
        */
    }

avengerPairs(avenger);

2 Comments

hey jaumeserr, I added a revised version using splicing to deal with duplication to my comment above. if it works for you, maybe mark the answer as correct?
@LSharpz Works perfectly! Thank you very very much!

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.