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
namesarray ?
nkshio
1,0801 gold badge14 silver badges23 bronze badges
-
Seems to properly generate 5 random objects to me. If you want 6 objects, just increase the iterations by one.CertainPerformance– CertainPerformance2018年08月25日 01:14:47 +00:00Commented Aug 25, 2018 at 1:14
-
Which one that you test? the php code is working but the javascript function code is not.Ace– Ace2018年08月25日 01:27:18 +00:00Commented Aug 25, 2018 at 1:27
-
Just press "Run code snippet". Your code already works.CertainPerformance– CertainPerformance2018年08月25日 01:27:47 +00:00Commented 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...Ace– Ace2018年08月25日 01:31:16 +00:00Commented Aug 25, 2018 at 1:31
-
Yes, turns out my code did work, thank you everybody.Ace– Ace2018年08月25日 01:49:15 +00:00Commented Aug 25, 2018 at 1:49
2 Answers 2
Resolution - Replace
i < names.length - 1withi < 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());
answered Aug 25, 2018 at 1:44
nkshio
1,0801 gold badge14 silver badges23 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Aug 25, 2018 at 1:26
H. Majury
3761 gold badge2 silver badges8 bronze badges
Comments
lang-js