Greetings from a newcomer to StackExchange! I recently delved into the world of programming and am currently learning JavaScript. My goal is to create an array of random numbers with 'n' size in order to experiment with sorting and searching algorithms to determine efficiency.
Despite my efforts, I seem to have hit a roadblock. I attempted this task yesterday but still cannot identify the root of the issue - my result is always an empty array.
My current setup involves using inquirer 0.12.0 for user input.
var inquirer = require('inquirer');
// GENERATE AN ARRAY WITH A SIZE OF n CONTAINING RANDOM NUMBERS
var value;
var limit;
var questionRandomSize = [{
type: 'input',
name: 'randomsize',
message: 'Enter the maximum number for randomness.',
validate: function (limit) {
if (limit > 0) {
return true;
} else {
return 'Please enter a number to proceed.'
}
}
}];
var questionArraySize = [{
type: 'input',
name: 'arraysize',
message: 'Enter the size of the array.',
validate: function (value) {
if (value > 0) {
return true;
} else {
return 'Please enter a number to proceed.';
}
}
}];
var nArray = new Array();
function generateArray(value) {
for (i = 0; i < value; i++) {
nArray.push(generateRandomNumber(limit));
}
}
inquirer.prompt(questionRandomSize, function (limit) {
function generateRandomNumber(limit) {
return Math.floor(Math.random() * limit);
}
inquirer.prompt(questionArraySize, function (value) {
generateArray(value);
mainMenu();
});
});
// MAIN MENU FUNCTION
function mainMenu() {
if (nArray.length === value){
console.log(nArray);
} else {
generateArray(value);
}
}
Thank you in advance for your assistance!