Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Section 2/Anatomy of a Node Application/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ app.printAJoke = function(){

// Pick a random number between 1 and the number of jokes
var randomNumber = mathLib.getRandomNumber(1,numberOfJokes);
if (randomNumber) {

// Get the joke at that position in the array (minus one)
var selectedJoke = allJokes[randomNumber - 1];
// Get the joke at that position in the array (minus one)
var selectedJoke = allJokes[randomNumber - 1];

// Send the joke to the console
console.log(selectedJoke);
// Send the joke to the console
console.log(selectedJoke);
} else {
console.log("No jokes in the file");
}
};


Expand Down
14 changes: 10 additions & 4 deletions Section 2/Anatomy of a Node Application/lib/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ var math = {};
// Get a random integer between two integers
// Inspired by: http://stackoverflow.com/questions/4959975/generate-random-number-between-two-numbers-in-javascript
math.getRandomNumber = function(min,max){
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0;
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0;
return Math.floor(Math.random()*(max-min+1)+min);

// Check that the min specified value is lower or equals the max one
if (min <= max) {
min = typeof(min) == 'number' && min % 1 === 0 ? min : 0;
max = typeof(max) == 'number' && max % 1 === 0 ? max : 0;
return Math.floor(Math.random()*(max-min+1)+min);
} else {
return false;
}
};


// Export the library
module.exports = math;
module.exports = math;