//tools.js
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomString;
}
exports.randomString = randomString();
//test.js
var tools = require('./tools');
console.log(tools.randomString());
But when I run it, I get this:
[Function: randomString]
How do I actually make it spit out a random string?
use a different name for your result. one which isn’t the same as your function’s name.
the variable
randomstringcontains your accumulated result but you returnrandomStringwhich is just off it by case and is the name of the function.