0

I want to create an array of objects with for loop using values in another array.

Code Snippet below generates 5 values, instead of 6 (as required)

function generateArray() {
    var names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    var newObj = [];

    for (i = 0; i < names.length - 1; i++) {
        newObj[i] = {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }
    }

    return newObj;
}

console.log(generateArray());

How do I generate as many values as present in names array ?

5
  • Seems to properly generate 5 random objects to me. If you want 6 objects, just increase the iterations by one. Commented Aug 25, 2018 at 1:14
  • Which one that you test? the php code is working but the javascript function code is not. Commented Aug 25, 2018 at 1:27
  • Just press "Run code snippet". Your code already works. Commented Aug 25, 2018 at 1:27
  • wait, how come it works when press run code snippet. I tried to do it in chrome it didn't work... Commented Aug 25, 2018 at 1:31
  • Yes, turns out my code did work, thank you everybody. Commented Aug 25, 2018 at 1:49

2 Answers 2

2

Resolution - Replace i < names.length - 1 with i < names.length

The condition for executing the code block in for loop is wrong. Your code works fine, just generates 1 less result than needed.

MDN web docs on how for works.

function generateArray() {
    names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    newObj = [];

    for (i = 0; i < names.length; i++) {
        newObj[i] = {
            name: names[(Math.floor(Math.random() * (names.length)))],
            age: Math.floor(Math.random() * 40),
            communication: Math.floor(Math.random() * 20),
            skill: Math.floor(Math.random() * 20),
            experience: Math.floor(Math.random() * 20)
        }
    }

    return newObj;
}

console.log(generateArray());

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

Comments

1
names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];

arob = [
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }, 
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }, 
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }, 
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }, 
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }, 
    {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20)
    }
];

Inside the for loop: i < names.length;, instead of i < names.length - 1;

function generateArray() {
    names = ["Fariz", "Falisha", "Mami", "Defina", "Fiska", "Papi"];
    newObj = [];

    for(i=0; i < names.length; i++) {
    newObj[i] = {
        name: names[(Math.floor(Math.random() * (names.length)))],
        age: Math.floor(Math.random() * 40),
        communication: Math.floor(Math.random() * 20),
        skill: Math.floor(Math.random() * 20),
        experience: Math.floor(Math.random() * 20) }
    }

    return newObj;
}

This returns an array with all 6 objects.

Comments

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.