Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  65] [ 5]  / answers: 1 / hits: 47357  / 8 Years ago, mon, december 19, 2016, 12:00:00

I have a project using node.js. It's my first time using nodejs and I want to export an array to my app. Here is some code:



module.exports = { 
var arrays = [];
arrays[0] = 'array 0';
arrays[1] = 'array 1';
arrays[2] = 'array 2';
arrays[3] = 'array 3';
arrays[4] = 'array 4';
var r_array = arrays[Math.floor(Math.random() * arrays.length)].toString();
}


At the end I want to use the var r_array in my app.js but I don't know how.


More From » arrays

 Answers
1

You'd want to define a function which returns the randomized portion of the array:



module.exports = {
getRArray: function() {
var arrays = [];
arrays[0] = 'array 0';
arrays[1] = 'array 1';
arrays[2] = 'array 2';
arrays[3] = 'array 3';
arrays[4] = 'array 4';
return arrays[Math.floor(Math.random()*arrays.length)];
}
};


Also you should embed the array into the function so it actually returns something.


[#59657] Friday, December 16, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucianod

Total Points: 667
Total Questions: 106
Total Answers: 92

Location: Jordan
Member since Thu, Aug 5, 2021
3 Years ago
;