Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialbrandonlind2
7,823 PointsDoes anyone know why this function isnt returning the array correctly?
Im a bit rusty on javascript and I'm loosely experimenting with dynamic item creation for practice, but I cant seem to figure out why the code past the for in loop isnt working how I want it to. help would be appreciated.
function WeaponGen(){
//grouping of item properties
var weaponGuards={
shape: ['oval','sqaure','straight cross','upward cross','curvy cross','basket'],
thickness:[ 'thick', 'medium', 'thin'],
length: ['long','short','medium']
};
var swordhandle={
shape:['oval','rectangular','square','circlular'],
length:['one handed', 'two handed']
};
(function randomWep(){
//cycles through all the objects
var traitTypes= [weaponGuards,swordhandle];
for(var i=0; i<traitTypes.length; i++){
traits=traitTypes[i];
//cycles through all the properties in the in the object in the above iteration
for(var key in traits){
randomTrait= Math.floor(Math.random() * key.length) +1;
trait=key[randomTrait];
console.log(trait);
}
//collects all item description parts into an array
var weapon=[];
weapon.push(trait)
}
return weapon;
})()
return weapon;
}
1 Answer
Jesus Mendoza
23,289 PointsHey Brandon,
Store the value returned from the function randomWep inside a variable and then return that variable from the weaponGen to see if it works
function weaponGen() {
// Code
function randomWeapon() {
// Code
return weapon;
}
var randomWeap = randomWeapon();
return randomWeap;
}
brandonlind2
7,823 Pointsbrandonlind2
7,823 PointsThanks jesus, I still having a problem were the weaponGen() returns random letters and a array with one random letter, I have no idea as to why.