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 trial

JavaScript

help on random generator

it is asking me to print a string with the length of the parameter I pass through it but i cant seem to get it to work can someone help me out?

var a = function(param1)
{
    var text = "";
    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var value = parseInt(param1);

    for( var i=0; i < value.length; i++ )
        text += possible.charAt(Math.floor(Math.random() * possible.length));

    return text;
}
a(5);
document.write(a());

1 Answer

Steven Parker
Steven Parker
243,656 Points

You're not treating your argument consistently with the requirements.

You said you needed "the length of the parameter I pass...", but you perform a parseInt on the parameter to turn it into a value, which doesn't seem like it would be be appropriate if you're only concerned about the length.

Then, your loop tries to take the length property of the value, but a numeric value would not have such a property.

Without seeing the actual assignment I can't be sure, but it appears that the loop should be using param1.length and the variable value isn't needed at all.

For enable the most accurate analysis, always provide a link to the page where the assignment was given.